当前位置:网站首页>Breadth first traversal based on adjacency table

Breadth first traversal based on adjacency table

2022-06-26 02:10:00 chengqiuming

One   Diagram to be created

Two   Code

package graph;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BFSAL {
    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(BFSAL.ALGraph G, char x) {
        for (int i = 0; i < G.vexnum; i++) //  Find the subscript of vertex information 
            if (x == G.Vex[i].data)
                return i;
        return -1; //  Did not find 
    }

    //  Insert an edge 
    static void insertedge(BFSAL.ALGraph G, int i, int j) {
        graph.AdjNode s = new graph.AdjNode();
        s.v = j;
        s.next = G.Vex[i].first;
        G.Vex[i].first = s;
    }

    //  Create a directed graph adjacency table 
    static void CreateALGraph(BFSAL.ALGraph G) {
        int i, j;
        char u, v;

        System.out.println(" Please enter the number of vertices and edges :");
        Scanner scanner = new Scanner(System.in);
        G.vexnum = scanner.nextInt();
        G.edgenum = scanner.nextInt();
        System.out.println(" Please enter vertex information :");

        for (i = 0; i < G.vexnum; i++)// Enter vertex information , Store vertex information array 
            G.Vex[i].data = scanner.next().charAt(0);
        for (i = 0; i < G.vexnum; i++)
            G.Vex[i].first = null;
        System.out.println(" Please enter two vertices of each edge in turn u,v");

        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)
                insertedge(G, i, j);
            else {
                System.out.println(" Input vertex information error ! Please re-enter !");
                G.edgenum++; //  This input does not count 
            }
        }
    }

    //  Output adjacency table 
    static void printg(BFSAL.ALGraph G) {
        System.out.println("---------- The adjacency table is as follows :----------");
        for (int i = 0; i < G.vexnum; i++) {
            graph.AdjNode t = G.Vex[i].first;
            System.out.print(G.Vex[i].data + ":  ");
            while (t != null) {
                System.out.print("[" + t.v + "]\t");
                t = t.next;
            }
            System.out.println();
        }
    }

    //  Breadth first traversal based on adjacency table 
    static void BFS_AL(ALGraph G, int v) {
        int u, w;
        graph.AdjNode p;
        //  Create a normal queue ( fifo ), Inside the store int type 
        Queue<Integer> Q = new LinkedList<>();
        System.out.println(G.Vex[v].data + "\t");

        visited[v] = true;
        Q.add(v); //  Source point  v  The team 
        //  If the queue is not empty 
        while (!Q.isEmpty()) {
            u = Q.peek(); //  Take out the team head element and assign it to u
            Q.poll(); //  Team leader element out of the team 
            p = G.Vex[u].first;
            while (p != null) { //  Check in turn u All the adjacency points of 
                w = p.v; // w  by  u  The adjacency point 
                if (!visited[w]) { // w  Not visited 
                    System.out.println(G.Vex[w].data + "\t");
                    visited[w] = true;
                    Q.add(w);
                }
                p = p.next;
            }
        }
    }

    public static void main(String[] args) {
        BFSAL.ALGraph G = new BFSAL.ALGraph();
        for (int i = 0; i < G.Vex.length; i++) {
            G.Vex[i] = new graph.VexNode();
        }
        CreateALGraph(G); //  Create a directed graph adjacency table 
        printg(G); //  Output adjacency table 
        System.out.println(" Please enter the starting point of the traversal graph :");
        Scanner scanner = new Scanner(System.in);
        char c = scanner.next().charAt(0);
        int v = locatevex(G, c);// Find vertices u Storage subscript for 
        if (v != -1) {
            System.out.println(" Breadth first search traversal graph results :");
            BFS_AL(G, v);
        } else
            System.out.println(" Input vertex information error ! Please re-enter !");
    }

    //  Define the adjacent contact type 
    static class AdjNode {
        int v; //  Adjacency subscript 
        graph.AdjNode next; //  Point to the next adjacent point 
    }

    //  Define vertex types 
    static class VexNode {
        char data; // VexType Is the data type of the vertex , Defined as needed 
        graph.AdjNode first; //  Point to the first adjacent contact 
    }

    //  Define adjacency table types 
    static class ALGraph {
        graph.VexNode Vex[] = new graph.VexNode[CreateALGraph.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/202206260036227607.html