当前位置:网站首页>Implementation of depth first traversal based on adjacency matrix

Implementation of depth first traversal based on adjacency matrix

2022-06-26 02:10:00 chengqiuming

One   Diagram to create

Two   Code

package graph;

import java.util.Scanner;

public class DFSAM {
    static final int MaxVnum = 100;  //  Maximum number of vertices 

    //  Access flag array , Its initial value is "false"
    static Boolean visited[] = new Boolean[MaxVnum];

    static {
        for (int i = 0; i < visited.length; i++) {
            visited[i] = false;
        }
    }

    static int locatevex(DFSAM.AMGraph G, char x) {
        for (int i = 0; i < G.vexnum; i++) //  Find the subscript of vertex information 
            if (x == G.Vex[i])
                return i;
        return -1; //  Did not find 
    }

    static void CreateAMGraph(DFSAM.AMGraph G) {
        Scanner scanner = new Scanner(System.in);
        int i, j;
        char u, v;
        System.out.println(" Please enter the number of vertices :");
        G.vexnum = scanner.nextInt();
        System.out.println(" Please enter the number of sides :");
        G.edgenum = scanner.nextInt();
        System.out.println(" Please enter vertex information :");

        //  Enter vertex information , Store vertex information array 
        for (int k = 0; k < G.vexnum; k++) {
            G.Vex[k] = scanner.next().charAt(0);
        }
        //  Initialize all values of adjacency matrix as 0, If it's a net , Then the initial adjacency matrix is infinite 
        for (int m = 0; m < G.vexnum; m++)
            for (int n = 0; n < G.vexnum; n++)
                G.Edge[m][n] = 0;

        System.out.println(" Please enter two vertices attached to each edge :");
        while (G.edgenum-- > 0) {
            u = scanner.next().charAt(0);
            v = scanner.next().charAt(0);

            i = locatevex(G, u);//  Find vertices  u  Storage subscript for 
            j = locatevex(G, v);//  Find vertices  v  Storage subscript for 
            if (i != -1 && j != -1)
                G.Edge[i][j] = 1; // Adjacency matrix storage 1
            else {
                System.out.println(" Input vertex information error ! Please re-enter !");
                G.edgenum++; //  This input does not count 
            }
        }
    }

    static void print(DFSAM.AMGraph G) { //  Output adjacency matrix 
        System.out.println(" The adjacency matrix of the graph is :");
        for (int i = 0; i < G.vexnum; i++) {
            for (int j = 0; j < G.vexnum; j++)
                System.out.print(G.Edge[i][j] + "\t");
            System.out.println();
        }
    }

    static void dfsAm(AMGraph G, int v) {// Depth first traversal based on adjacency matrix 
        int w;
        System.out.println(G.Vex[v] + "\t");


        visited[v] = true;
        for (w = 0; w < G.vexnum; w++) //  Check in turn v All the adjacency points of 
            if (G.Edge[v][w] == 1 && !visited[w]) // v、w  Adjacency and  w  Not visited 
                dfsAm(G, w); //  from  w  Vertex start recursive depth first traversal 
    }

    public static void main(String[] args) {
        int v;
        char c;
        AMGraph G = new DFSAM.AMGraph();
        CreateAMGraph(G);
        print(G);
        System.out.println(" Please enter the starting point of traversing the connected graph :");
        Scanner scanner = new Scanner(System.in);
        c = scanner.next().charAt(0);
        v = locatevex(G, c); //  Find vertices u Storage subscript for 
        if (v != -1) {
            System.out.println(" Depth first search traversal connected graph results :");
            dfsAm(G, v);
        } else
            System.out.println(" Input vertex information error ! Please re-enter !");
    }

    static class AMGraph {
        char Vex[] = new char[CreateAMGraph.MaxVnum];
        int Edge[][] = new int[CreateAMGraph.MaxVnum][CreateAMGraph.MaxVnum];
        int vexnum; //  Number of vertices 
        int edgenum; //  Number of edges 
    }
}

3、 ... and   test

Green is the input , White is the output

原网站

版权声明
本文为[chengqiuming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260036227415.html