-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
122 lines (110 loc) · 3.57 KB
/
Graph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class Graph {
int[][] adjMatrix;
int vCount;
boolean[] visited;
Graph(int vCount) {
this.vCount = vCount;
adjMatrix = new int[vCount][vCount];
visited = new boolean[vCount];
}
void readEdges(Scanner scanner) {
System.out.println("Enter edges (source destination): ");
for (int i = 0; i < vCount; i++) {
for (int j = 0; j < vCount; j++) {
System.out.print("Enter edge " + (i + 1) + " to " + (j + 1) + ": ");
adjMatrix[i][j] = scanner.nextInt();
}
}
}
void displayAdjacency() {
System.out.println("Adjacency Matrix:");
for (int i = 0; i < vCount; i++) {
for (int j = 0; j < vCount; j++) {
System.out.print(adjMatrix[i][j] + " ");
}
System.out.println();
}
}
void BFS(int start) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(start);
visited[start] = true;
System.out.print("BFS: ");
while (!queue.isEmpty()) {
int current = queue.poll();
System.out.print(current + " ");
for (int i = 0; i < vCount; i++) {
if (adjMatrix[current][i] == 1 && !visited[i]) {
queue.offer(i);
visited[i] = true;
}
}
}
System.out.println();
}
void DFS(int start) {
Stack<Integer> stack = new Stack<>();
stack.push(start);
visited[start] = true;
System.out.print("DFS: ");
while (!stack.isEmpty()) {
int current = stack.pop();
System.out.print(current + " ");
for (int i = 0; i < vCount; i++) {
if (adjMatrix[current][i] == 1 && !visited[i]) {
stack.push(i);
visited[i] = true;
}
}
}
System.out.println();
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("Menu:");
System.out.println("1. Perform Breadth First Search(BFS)");
System.out.println("2. Perform Depth First Search(DFS)");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
performSearch(scanner, choice);
break;
case 2:
performSearch(scanner, choice);
break;
case 3:
exit = true;
break;
default:
System.out.println("Invalid choice");
}
}
}
public static void performSearch(Scanner scanner, int choice) {
System.out.print("Enter vertex count: ");
int vCount = scanner.nextInt();
Graph graph = new Graph(vCount);
graph.readEdges(scanner);
graph.displayAdjacency();
System.out.print("Enter starting vertex: ");
int startVertex = scanner.nextInt();
switch (choice) {
case 1:
graph.BFS(startVertex - 1); // Adjust for 0-based indexing
break;
case 2:
graph.DFS(startVertex - 1); // Adjust for 0-based indexing
break;
}
}
}