forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
56 lines (43 loc) · 1.5 KB
/
Graph.java
File metadata and controls
56 lines (43 loc) · 1.5 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
/**
* The graph structre represented using adjacency lists.
*/
package graphalgorithms;
import java.util.List;
import java.util.Arrays;
public class Graph {
public static void main(String[] args) {
int numberOfVertex = 10;
int[][] adjacencyList = {
{0, 1, 6, 8},
{1, 4, 6, 9},
{2, 4, 6},
{3, 4, 5, 8},
{4, 5, 9},
{7, 8, 9}
};
List<Vertex> vertices = AdjacencyList.build(adjacencyList, numberOfVertex);
AdjacencyList.print(vertices);
Graph graph = new Graph(vertices);
BreadthFirstSearch bfSearch = new BreadthFirstSearch();
Vertex source = graph.getVertex(0);
Vertex destination = graph.getVertex(5);
bfSearch.bfs(graph, source);
System.out.printf("print path from %s to %s%n", source, destination);
bfSearch.printPath(graph, source, destination);
DepthFirstSearch dfSearch = new DepthFirstSearch();
dfSearch.dfs(graph);
TopologicalSort sort = new TopologicalSort();
List<Vertex> topologicalSortList = sort.topologicalSort(graph);
System.out.printf("Topological Sort: %s%n", Arrays.toString(topologicalSortList.toArray()));
}
private final List<Vertex> vertices;
public Graph(List<Vertex> vertices) {
this.vertices = vertices;
}
public List<Vertex> getVertices() {
return vertices;
}
public Vertex getVertex(int i) {
return vertices.get(i);
}
}