Figure traverse the breadth and depth of traversal


Is there a map of the route can go from the starting point (upper left corner of figure) (reach another point, usually the lower right corner of the map)

Import java.util.ArrayList;
Import java.util.Arrays;
Import Classes in java.util .LinkedList;

FIG //
public class Graph {
     Private the ArrayList <String> vertextList; // store the set of vertices
     private int [] [] edges; // memory map corresponding to the matrix of neighbor
     private int numOfEdges; // represents the number of edges
     private boolean [] isVisited;


     public static void main(String[] args) {

        int n = 5;//结点个数
         String vertexs[] = {"A", "B", "C", "D", "E"};
         Graph graph = new Graph(n);
         //添加结点
         for (int i = 0; i < vertexs.length; i++) {
             graph.insertVertex(vertexs[i]);
         }
         //添加边
         //A-B  A-C  B-C  B-D  B-E
         graph.insertEdge(0, 1, 1);
         graph.insertEdge(0, 2, 1);
         graph.insertEdge(1, 2, 1);
         graph.insertEdge(1, 3, 1);
         graph.insertEdge(1, 4, 1);

        graph.showGraph ();
         System.out.println ( "\ n-deep traversal");
         graph.dfs ();


         System.out.println ( "\ n breadth traversal");
         graph.bfs ();


     }

    public Graph(int n) {
         this.edges = new int[n][n];
         this.vertextList = new ArrayList<String>(n);
         this.numOfEdges = 0;

    }

    // traverse all nodes, breadth-first traversal
     Private void BFS () {
         isVisited = new new Boolean [. 5];
         for (int I = 0; I <getNumOfVertex (); I ++) {
             IF (! IsVisited [I]) {
                 BFS (isVisited, I);
             }
         }

    }

    // node to a breadth-first traversal
     Private void BFS (Boolean [] isVisited, I int) {
         int U; // represents the head of the queue node
         int w; // adjacent node
         // queue
         LinkedList <Integer> queue the LinkedList new new = <> ();
         of System.out.print (getValueByIndex (I) + "->");
         isVisited [I] = to true;
         // Add the node queue
         queue.add (I);
         the while (queue! .isEmpty ()) {
             // get the head of the queue node
             U = queue.removeFirst ();
             // get adjacent node
             W = getFirstNeighbor (U);
             ! the while (W = -1) {
                 ! IF (isVisited [W ]) {
                     of System.out.print (getValueByIndex (W) + "->");
                     isVisited [W] = to true;
                     // enqueue
                     queue.addLast (W);
                 }
                 // W has visited, and to find a next adjacent node
                 W = getNextNeighbor (U, W);
             }
         }

    }


     // overloaded dfs, traversing all nodes
     public void DFS () {
         isVisited new new Boolean = [. 5];
         for (int I = 0; I <getNumOfVertex (); I ++) {
             (! IsVisited [I]) {IF
                 DFS (isVisited, I);
             }
         }
     }


     // depth-first traversal algorithm
     Private void DFS (Boolean [] isVisited, I int) {
         // access
         of System.out.print (getValueByIndex (I) + "->");
         // set the node has access
         isVisited [ i] = to true;
         // find contact collar node i
         int getFirstNeighbor = W (i);
         the while (W = -1!) {
             IF (isVisited [W]!) {
                 DFS (isVisited, W);
             }
             // If w has been visited
             w = getNextNeighbor (I, w);
         }

    }


     / **
      * get the first adjacent node of the subscript
      *
      * @param index given node
      * @return neighbor node returns subscript
      * /
     public int getFirstNeighbor (int index) {
         for (int I = 0; I <vertextList.size (); I ++) {
             IF (Edges [index] [I]> 0) {
                 return I;
             }
         }
         return -1;
     }

    The subscripts // acquired before a node adjacent to the next adjacent node
     public int getNextNeighbor (int V1, V2 int) {
         for (int I = V2 +. 1; I <vertextList.size (); I ++) {
             IF (Edges [V1] [I]> 0) {
                 return I;
             }
         }
         return -1;
     }


     //插入结点
     public void insertVertex(String vertex) {
         vertextList.add(vertex);
     }


     / **
      * insertion edge
      *
      * @param V1 denotes a first index point
      * @param v2 represents the second index point
      * @param weight weight
      * /
     public void insertEdge (int V1, V2 int, int weight) {
         Edges [V1] [V2] = weight;
         Edges [V2] [V1] = weight;
         numOfEdges ++;
     }

    public int getNumOfVertex() {
         return vertextList.size();
     }

    public int getNumOfEdges() {
         return numOfEdges;
     }

    // return value of a node
     public String getValueByIndex (I int) {
         return vertextList.get (I);
     }

    // returns weights
     public int getWeight (int V1, V2 int) {
         return Edges [V1] [V2];
     }

    // matrix showing
     public void showGraph () {
         for (int [] Edge: Edges) {
             System.out.println (of Arrays.toString (Edge));
         }
     }


}

Guess you like

Origin www.cnblogs.com/jf510/p/11826390.html