-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand.java
More file actions
104 lines (84 loc) · 3.3 KB
/
rand.java
File metadata and controls
104 lines (84 loc) · 3.3 KB
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
import java.io.*;
import java.util.*;
public class rand {
static class Edge {
int node;
int cost;
Edge(int node, int cost) {
this.node = node;
this.cost = cost;
}
Edge(int node) {
this.node = node;
this.cost = 0; // Default cost
}
}
private static int N; // Number of nodes
private static int M; // Number of edges
private static Map<Integer, List<Edge>> graph = new HashMap<>();
public static void main(String[] args) throws IOException {
String fileName = "graph.txt"; // Replace with your file name
boolean includeCosts = true; // Set to true if the costs are included in the file
readGraph(fileName, includeCosts);
// Perform DFS and BFS
System.out.println("DFS Traversal:");
dfs(0); // Starting from node 0
System.out.println("\nBFS Traversal:");
bfs(0); // Starting from node 0
}
private static void readGraph(String fileName, boolean includeCosts) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String[] firstLine = br.readLine().trim().split("\\s+");
N = Integer.parseInt(firstLine[0]);
M = Integer.parseInt(firstLine[1]);
for (int i = 0; i < M; i++) {
String[] parts = br.readLine().trim().split("\\s+");
int u = Integer.parseInt(parts[0]);
int v = Integer.parseInt(parts[1]);
if (includeCosts && parts.length == 3) {
int cost = Integer.parseInt(parts[2]);
addEdge(u, v, cost);
} else {
addEdge(u, v);
}
}
br.close();
}
private static void addEdge(int u, int v) {
graph.computeIfAbsent(u, k -> new ArrayList<>()).add(new Edge(v));
graph.computeIfAbsent(v, k -> new ArrayList<>()).add(new Edge(u)); // Assuming undirected graph
}
private static void addEdge(int u, int v, int cost) {
graph.computeIfAbsent(u, k -> new ArrayList<>()).add(new Edge(v, cost));
graph.computeIfAbsent(v, k -> new ArrayList<>()).add(new Edge(u, cost)); // Assuming undirected graph
}
private static void dfs(int start) {
boolean[] visited = new boolean[N];
dfsUtil(start, visited);
}
private static void dfsUtil(int node, boolean[] visited) {
visited[node] = true;
System.out.print(node + " ");
for (Edge edge : graph.getOrDefault(node, Collections.emptyList())) {
if (!visited[edge.node]) {
dfsUtil(edge.node, visited);
}
}
}
private static void bfs(int start) {
boolean[] visited = new boolean[N];
Queue<Integer> queue = new LinkedList<>();
visited[start] = true;
queue.offer(start);
while (!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
for (Edge edge : graph.getOrDefault(node, Collections.emptyList())) {
if (!visited[edge.node]) {
visited[edge.node] = true;
queue.offer(edge.node);
}
}
}
}
}