Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e46539

Browse files
Added BREADTH FIRST TRAVERSAL (First Layer)
1 parent b1ce94f commit 8e46539

File tree

1 file changed

+81
-0
lines changed
  • Graph Data Structures/Graph Traversal/Breadth-First Traversal/Breadth-First Traversal (First layer)

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Traversing down one layer
2+
3+
// USE GRAPH FILE FROM GRAPH FOLDER
4+
5+
import java.util.ArrayList;
6+
7+
class GraphTraverser {
8+
public static void depthFirstTraversal(Vertex start, ArrayList<Vertex> visitedVertices) {
9+
System.out.println(start.getData());
10+
11+
for (Edge e: start.getEdges()) {
12+
Vertex neighbor = e.getEnd();
13+
14+
if (!visitedVertices.contains(neighbor)) {
15+
visitedVertices.add(neighbor);
16+
GraphTraverser.depthFirstTraversal(neighbor, visitedVertices);
17+
}
18+
}
19+
}
20+
21+
public static void breadthFirstTraversal(Vertex start, ArrayList<Vertex> visitedVertices) {
22+
for(Edge e : start.getEdges()) {
23+
Vertex neighbor = e.getEnd();
24+
if(!visitedVertices.contains(neighbor)) {
25+
visitedVertices.add(neighbor);
26+
}
27+
}
28+
29+
System.out.println(visitedVertices);
30+
}
31+
32+
// USE IT TO TEST GRAPH
33+
class TestGraph {
34+
private Graph testGraph;
35+
36+
public TestGraph() {
37+
this.testGraph = new Graph(false, true);
38+
Vertex startNode = testGraph.addVertex("v0.0.0");
39+
Vertex v1 = this.testGraph.addVertex("v1.0.0");
40+
Vertex v2 = this.testGraph.addVertex("v2.0.0");
41+
42+
Vertex v11 = this.testGraph.addVertex("v1.1.0");
43+
Vertex v12 = this.testGraph.addVertex("v1.2.0");
44+
Vertex v21 = this.testGraph.addVertex("v2.1.0");
45+
46+
Vertex v111 = this.testGraph.addVertex("v1.1.1");
47+
Vertex v112 = this.testGraph.addVertex("v1.1.2");
48+
Vertex v121 = this.testGraph.addVertex("v1.2.1");
49+
Vertex v211 = this.testGraph.addVertex("v2.1.1");
50+
51+
this.testGraph.addEdge(startNode, v1, null);
52+
this.testGraph.addEdge(startNode, v2, null);
53+
54+
this.testGraph.addEdge(v1, v11, null);
55+
this.testGraph.addEdge(v1, v12, null);
56+
this.testGraph.addEdge(v2, v21, null);
57+
58+
this.testGraph.addEdge(v11, v111, null);
59+
this.testGraph.addEdge(v11, v112, null);
60+
this.testGraph.addEdge(v12, v121, null);
61+
this.testGraph.addEdge(v21, v211, null);
62+
63+
// create a cycle
64+
this.testGraph.addEdge(v211, v2, null);
65+
}
66+
67+
public Vertex getStartingVertex() {
68+
return this.testGraph.getVertices().get(0);
69+
}
70+
}
71+
72+
// MAIN METHOD
73+
public static void main(String[] args) {
74+
TestGraph test = new TestGraph();
75+
Vertex startingVertex = test.getStartingVertex();
76+
ArrayList<Vertex> visitedVertices = new ArrayList<Vertex>();
77+
visitedVertices.add(startingVertex);
78+
79+
GraphTraverser.breadthFirstTraversal(startingVertex, visitedVertices);
80+
}
81+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /