-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstSearch.java
More file actions
155 lines (134 loc) · 3.51 KB
/
DepthFirstSearch.java
File metadata and controls
155 lines (134 loc) · 3.51 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.*;
import java.util.*;
public class DepthFirstSearch {
boolean[] visited;
Graph graph;
int nodeAccessCount = 0;
public DepthFirstSearch(Graph g) {
visited = new boolean[g.getVerticesCount()];
graph = g;
}
public void init() {
for(int i = 0; i < graph.getNodes().size(); i++) {
if(visited[i] == false) {
dfs(i);
}
}
System.err.println("Total access: "+ nodeAccessCount);
System.err.println("Access/NodeCount = "+ nodeAccessCount/graph.getVerticesCount());
}
public void dfs(int vertex) {
visited[vertex] = true;
nodeAccessCount++;
Graph.Node node = graph.getNodes().get(vertex);
if(node.vertex == vertex) {
Iterator<Integer> edgeIterator = node.getEdges().listIterator();
System.err.println(vertex +" ");
while(edgeIterator.hasNext()) {
Integer nextNode = edgeIterator.next();
if(!visited[nextNode]) {
dfs(nextNode);
}
}
}
}
static class Graph{
int numVertices;
int numEdges;
ArrayList<Graph.Node> nodes;
Graph(List<String> data){
createNodes(data);
numVertices = nodes.size();
numEdges = countEdges();
}
public void createNodes(List<String> data) {
nodes = new ArrayList<Graph.Node>();
for(int i = 0; i < data.size(); i++) {
String vertex = data.get(i).split(":")[0];
String edgesString ="";
String[] edges = new String[0];
if(data.get(i).split(":").length > 1) {
edgesString = data.get(i).split(":")[1].trim();
edges = new String[edgesString.split(",").length];
for(int x = 0; x <= (edgesString.split(",").length-1); x++) {
edges[x] = edgesString.split(",")[x];
}
}
try {
nodes.add(new Node(vertex, edges));
}
catch(Exception e) {
System.err.println("Unable to create new Node.");
throw e;
}
}
}
public int countEdges(){
int count = 0;
for(int i = 0; i < nodes.size(); i++){
Node node = nodes.get(i);
count += node.getEdges().size();
}
return count;
}
public int getVerticesCount() {
return this.numVertices;
}
public ArrayList<Graph.Node> getNodes(){
return this.nodes;
}
public int getEdgeCount() {
return this.numEdges;
}
class Node{
int vertex;
ArrayList<Integer> edges;
Node(String vertex_data, String[] edges_data){
try {
this.vertex = Integer.parseInt(vertex_data);
this.edges = new ArrayList<Integer>();
for(int i = 0; i < edges_data.length; i++) {
this.edges.add(Integer.parseInt(edges_data[i]));
}
}
catch(Exception e) {
e.printStackTrace();
throw e;
}
}
public ArrayList<Integer> getEdges(){
return this.edges;
}
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
if(args.length < 1){
System.err.println("Provide a file as argument");
System.exit(0);
}
String fileName = args[0];
String line = null;
DepthFirstSearch dfs;
Graph graph;
List<String> unstructuredNodes = new ArrayList<String>();
try{
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null){
unstructuredNodes.add(line);
}
graph = new Graph(unstructuredNodes);
dfs = new DepthFirstSearch(graph);
dfs.init();
bufferedReader.close();
}
catch(FileNotFoundException ex){
System.err.println("Unable to open file "+fileName);
}
catch(IOException ex){
System.err.println("Error reading file");
ex.printStackTrace();
}
}
}