当前位置:网站首页>Largeur d'abord traversée basée sur la matrice de contiguïté
Largeur d'abord traversée basée sur la matrice de contiguïté
2022-06-26 02:10:00 【Chengqiuming】
Un. Diagramme à créer

2. Code
package graph;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BFSAM {
static final int MaxVnum = 100; // Nombre maximum de sommets
// Tableau des drapeaux d'accès,Sa valeur initiale est"false"
static Boolean visited[] = new Boolean[MaxVnum];
static {
for (int i = 0; i < visited.length; i++) {
visited[i] = false;
}
}
static int locatevex(BFSAM.AMGraph G, char x) {
for (int i = 0; i < G.vexnum; i++) // Trouver un indice pour les informations Vertex
if (x == G.Vex[i])
return i;
return -1; // Je n'ai pas trouvé
}
static void CreateAMGraph(BFSAM.AMGraph G) {
Scanner scanner = new Scanner(System.in);
int i, j;
char u, v;
System.out.println("Veuillez entrer le nombre de sommets:");
G.vexnum = scanner.nextInt();
System.out.println("Veuillez saisir le nombre de bords:");
G.edgenum = scanner.nextInt();
System.out.println("Veuillez saisir les informations du Vertex:");
// Saisissez les informations Vertex, Enregistrer dans le tableau d'information Vertex
for (int k = 0; k < G.vexnum; k++) {
G.Vex[k] = scanner.next().charAt(0);
}
// Initialiser la matrice de contiguïté toutes les valeurs sont 0,Si c'est un filet, Initialiser la matrice de contiguïté à l'infini
for (int m = 0; m < G.vexnum; m++)
for (int n = 0; n < G.vexnum; n++)
G.Edge[m][n] = 0;
System.out.println(" Veuillez saisir les deux sommets attachés à chaque bord :");
while (G.edgenum-- > 0) {
u = scanner.next().charAt(0);
v = scanner.next().charAt(0);
i = locatevex(G, u);// Trouver des sommets u Indice de stockage pour
j = locatevex(G, v);// Trouver des sommets v Indice de stockage pour
if (i != -1 && j != -1)
G.Edge[i][j] = 1; // Stockage de la matrice de contiguïté 1
else {
System.out.println(" Erreur lors de l'entrée des informations Vertex !Veuillez saisir à nouveau!");
G.edgenum++; // Cette entrée ne compte pas
}
}
}
static void print(BFSAM.AMGraph G) { // Matrice de contiguïté de sortie
System.out.println("La matrice de contiguïté du graphique est:");
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();
}
}
// Largeur d'abord traversée basée sur la matrice de contiguïté
static void BFS_AM(AMGraph G, int v) {
int u, w;
// Créer une file d'attente normale (Premier entré, premier sorti),À l'intérieur.intType
Queue<Integer> Q = new LinkedList<>();
System.out.println(G.Vex[v] + "\t");
visited[v] = true;
Q.add(v); // Source v En ligne.
while (!Q.isEmpty()) { //Si la file d'attente n'est pas vide
u = Q.peek(); // Retirez l'élément de tête de file d'attente et attribuez - le àu
Q.poll(); // Chef d'équipe hors de l'équipe
for (w = 0; w < G.vexnum; w++) {// Vérifiez à tour de rôle u Tous les points adjacents à
if (G.Edge[u][w] == 1 && !visited[w]) { // u、w Contiguë et w Non visité
System.out.println(G.Vex[w] + "\t");
visited[w] = true;
Q.add(w);
}
}
}
}
public static void main(String[] args) {
int v;
char c;
AMGraph G = new BFSAM.AMGraph();
CreateAMGraph(G);
print(G);
System.out.println(" Veuillez entrer le point de départ du diagramme de traversée :");
Scanner scanner = new Scanner(System.in);
c = scanner.next().charAt(0);
v = locatevex(G, c); // Trouver des sommetsu Indice de stockage pour
if (v != -1) {
System.out.println(" Largeur d'abord rechercher les résultats de la traversée :");
BFS_AM(G, v);
} else
System.out.println(" Erreur lors de l'entrée des informations Vertex !Veuillez saisir à nouveau!");
}
static class AMGraph {
char Vex[] = new char[CreateAMGraph.MaxVnum];
int Edge[][] = new int[CreateAMGraph.MaxVnum][CreateAMGraph.MaxVnum];
int vexnum; // Nombre de sommets
int edgenum; // Nombre de bords
}
}
Trois Tests

边栏推荐
猜你喜欢

vs2015+PCL1.8.1+qt5.12-----(1)

Implementation of image binary morphological filtering based on FPGA -- Corrosion swelling

cv==biaoding---open----cv001

【js】免费api判断节假日、工作日和周六日

About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi

Exploring temporary information for dynamic network embedding

Disruptor (I) sequence

Ndk20b ffmpeg4.2.2 compilation and integration

win32
![[JS] free API to judge holidays, working days, Saturdays and Sundays](/img/e1/8b742082385bb5e60f74beb3b81c7d.png)
[JS] free API to judge holidays, working days, Saturdays and Sundays
随机推荐
高手常用的电脑快捷键
CVPR2022 | 长期行动预期的Future Transformer
深度好文:什么是超网 Supernetting?
Connecting the projector
Prompt to update to the latest debug version during vscode debugging
Exploring temporary information for dynamic network embedding
Getting to know OpenGL
Simplex method (1)
FPGA实现图像二值形态学滤波——腐蚀膨胀
Redis7.0 installation steps
Raspberry pie + AWS IOT Greengrass
【无标题】vsbiji esp....32
Disruptor (I) sequence
vtk初始化代码学习1
樹莓派 + AWS IoT Greengrass
缓存技术之第一次亲密接触
shell curl 执行脚本,带传参数,自定义参数
A lost note for konjaku beginner
Connectez Le projecteur
[JS] free API to judge holidays, working days, Saturdays and Sundays