zoukankan      html  css  js  c++  java
  • 图-分有向图和没向图

        1

    /    |      

    2    3  —   5

    4

    1.输出遍历的路径(图深度优先搜索实现)

    //1代表有边,5代表没边
    int
    book[101],sum,n=6,m=6,e[101][101] = { {0,0,0,0,0,0}, {0,0,1,1,5,1}, {0,1,0,5,1,5}, {0,1,5,0,5,1}, {0,5,1,5,0,5}, {0,1,5,1,5,0}}; void dfs(int cur);//cur当前定点编号

    .

    //图-深度优先搜索
            book[1] = 1;
            dfs(1);

    .

    void dfs(int cur){
        printf("%d	",cur);
        sum++;
        if (sum==n) return;
        for (int i = 1; i <= n; i++) {
            if (e[cur][i]==1 && book[i]==0) {
                book[i] = 1;
                dfs(i);
            }
        }
        
    }//cur当前定点编号

    2.输出遍历的路径(图的广度优先搜索实现)

    //广度优先搜索 =(队列+hashMap)
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            //图-广度优先搜索
            int que[10001],head = 1,tail = 1,cur = 0;
            que[tail] = 1;
            tail++;
            book[1] = 1;
            
            while (head < tail)
            {
                cur = que[head];
                for (int i = 1; i <= n; i++)
                {
                    //有边的情况下,入队
                    if (e[cur][i]==1 && book[i]==0) {
                        
                        que[tail] = i;
                        tail++;
                        book[i] = 1;
                    }
                    if (tail>n) {
                        break;
                    }
                }
                head++;
            }
            for (int i = 1; i < tail; i++) {
                printf("%d	",que[i]);
            }
        }
        return 0;
    }

     2.通过值为1的节点找到值为5的节点(图的广度优先搜索实现)

    package com.company;
    import java.util.LinkedList;
    import java.util.Queue;
    
    
    class GraphNode
    {
        int val;
        GraphNode next;//
        GraphNode[] neighbors;//
        boolean visited;
    
        GraphNode(int x)
        {
            val = x;
        }
        //
        GraphNode(int x,GraphNode[] n)
        {
            val = x;
            neighbors = n;
        }
    }
    
    
    public class Main {
    
        public static void main(String[] args) {
        // write your code here
            GraphNode n1 = new GraphNode(1);
            GraphNode n2 = new GraphNode(2);
            GraphNode n3 = new GraphNode(3);
            GraphNode n4 = new GraphNode(4);
            GraphNode n5 = new GraphNode(5);
            //
            n1.neighbors = new GraphNode[]{n2,n3,n5};
            n2.neighbors = new GraphNode[]{n1,n4};
            n3.neighbors = new GraphNode[]{n1,n4,n5};
            n4.neighbors = new GraphNode[]{n2,n3,n5};
            n5.neighbors = new GraphNode[]{n1,n3,n4};
            //
            breathFirstSearch(n1,5);
    
        }
    
        public static void breathFirstSearch(GraphNode root,int x)
        {
            if (root.val == x)
            {
                System.out.printf("find in root");
            }
    
            Queue<GraphNode> queue = new LinkedList<>();
            root.visited = true;
            queue.offer(root);
    
            while (!queue.isEmpty())
            {
                GraphNode c = queue.poll();
                for (GraphNode n : c.neighbors)
                {
                    if (!n.visited)
                    {
                        System.out.printf("%d ",n.val);
                        n.visited = true;
                        if (n.val == x)
                        {
                            System.out.printf("
    find result %d",n.val);
                        }
                    }
                }
            }
        }
        
    }
  • 相关阅读:
    小梦windows phone 8.1开发:语音朗读
    windows phone 8.1开发:socket通信聊天
    windows phone 8.1开发:触控和指针事件1
    Windows Phone 8.1开发:触控和指针事件2
    windows phone 8.1开发 onedrive操作详解
    windows phone 8.1开发SQlite数据库引用安装
    利用Register protocol实现网页调用桌面程序(类似迅雷、QQ等)
    windows phone 8.1开发SQlite数据库操作详解
    xrandr
    mongodb
  • 原文地址:https://www.cnblogs.com/huen/p/4980714.html
Copyright © 2011-2022 走看看