Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 226b281

Browse files
committed
Added Breadth First Traversal (All Layers)
1 parent 8e46539 commit 226b281

File tree

1 file changed

+127
-0
lines changed
  • Graph Data Structures/Graph Traversal/Breadth-First Traversal/Breadth-First Traversal (All layers)

1 file changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// USE GRAPH FILE FROM GRAPH FOLDER
2+
3+
import java.util.ArrayList;
4+
5+
class GraphTraverser {
6+
// QUEUE
7+
public class Queue {
8+
9+
public LinkedList queue;
10+
public int size;
11+
12+
public Queue() {
13+
this.queue = new LinkedList();
14+
this.size = 0;
15+
}
16+
17+
public boolean isEmpty() {
18+
return this.size == 0;
19+
}
20+
21+
public void enqueue(Vertex data) {
22+
this.queue.addToTail(data);
23+
this.size++;
24+
}
25+
26+
public Vertex peek() {
27+
if (this.isEmpty()) {
28+
return null;
29+
} else {
30+
return this.queue.head.data;
31+
}
32+
}
33+
34+
public Vertex dequeue() {
35+
if (!this.isEmpty()) {
36+
Vertex data = this.queue.removeHead();
37+
this.size--;
38+
return data;
39+
} else {
40+
throw new Error("LinearDataStructures.Queues.Queue is empty!");
41+
}
42+
}
43+
44+
}
45+
46+
// GRAPH TRAVERSER
47+
public static void depthFirstTraversal(Vertex start, ArrayList<Vertex> visitedVertices) {
48+
System.out.println(start.getData());
49+
50+
for (Edge e: start.getEdges()) {
51+
Vertex neighbor = e.getEnd();
52+
53+
if (!visitedVertices.contains(neighbor)) {
54+
visitedVertices.add(neighbor);
55+
GraphTraverser.depthFirstTraversal(neighbor, visitedVertices);
56+
}
57+
}
58+
}
59+
60+
public static void breadthFirstTraversal(Vertex start, ArrayList<Vertex> visitedVertices) {
61+
Queue visitQueue = new Queue();
62+
visitQueue.enqueue(start);
63+
64+
while (!visitQueue.isEmpty()) {
65+
Vertex current = visitQueue.dequeue();
66+
System.out.println(current.getData());
67+
68+
for(Edge e : current.getEdges()) {
69+
Vertex neighbor = e.getEnd();
70+
if(!visitedVertices.contains(neighbor)) {
71+
visitQueue.enqueue(neighbor);
72+
visitedVertices.add(neighbor);
73+
}
74+
}
75+
}
76+
}
77+
78+
// USE IT TO TEST GRAPH
79+
class TestGraph {
80+
private Graph testGraph;
81+
82+
public TestGraph() {
83+
this.testGraph = new Graph(false, true);
84+
Vertex startNode = testGraph.addVertex("v0.0.0");
85+
Vertex v1 = this.testGraph.addVertex("v1.0.0");
86+
Vertex v2 = this.testGraph.addVertex("v2.0.0");
87+
88+
Vertex v11 = this.testGraph.addVertex("v1.1.0");
89+
Vertex v12 = this.testGraph.addVertex("v1.2.0");
90+
Vertex v21 = this.testGraph.addVertex("v2.1.0");
91+
92+
Vertex v111 = this.testGraph.addVertex("v1.1.1");
93+
Vertex v112 = this.testGraph.addVertex("v1.1.2");
94+
Vertex v121 = this.testGraph.addVertex("v1.2.1");
95+
Vertex v211 = this.testGraph.addVertex("v2.1.1");
96+
97+
this.testGraph.addEdge(startNode, v1, null);
98+
this.testGraph.addEdge(startNode, v2, null);
99+
100+
this.testGraph.addEdge(v1, v11, null);
101+
this.testGraph.addEdge(v1, v12, null);
102+
this.testGraph.addEdge(v2, v21, null);
103+
104+
this.testGraph.addEdge(v11, v111, null);
105+
this.testGraph.addEdge(v11, v112, null);
106+
this.testGraph.addEdge(v12, v121, null);
107+
this.testGraph.addEdge(v21, v211, null);
108+
109+
// create a cycle
110+
this.testGraph.addEdge(v211, v2, null);
111+
}
112+
113+
public Vertex getStartingVertex() {
114+
return this.testGraph.getVertices().get(0);
115+
}
116+
}
117+
118+
// MAIN METHOD
119+
public static void main(String[] args) {
120+
TestGraph test = new TestGraph();
121+
Vertex startingVertex = test.getStartingVertex();
122+
ArrayList<Vertex> visitedVertices = new ArrayList<Vertex>();
123+
visitedVertices.add(startingVertex);
124+
125+
GraphTraverser.breadthFirstTraversal(startingVertex, visitedVertices);
126+
}
127+
}}

0 commit comments

Comments
 (0)