-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphAdjacencyList.java
More file actions
123 lines (83 loc) · 2.71 KB
/
GraphAdjacencyList.java
File metadata and controls
123 lines (83 loc) · 2.71 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
import java.util.*;
public class GraphAdjacencyList {
private Map<Integer,ArrayList<Integer>> parisMap;
public GraphAdjacencyList(int vertices){
parisMap = new HashMap<Integer,ArrayList<Integer>>();
for(int i=0;i<=vertices;i++){
ArrayList<Integer> neighbours = new ArrayList<Integer>();
parisMap.put(i, neighbours);
}
}
public void addEdge(int v, int w){
if(v > parisMap.size() || w > parisMap.size()){
return;
}
(parisMap.get(v)).add(w);
(parisMap.get(w)).add(v);
}
public ArrayList<Integer> getNeighbours(int v){
if(v>parisMap.size()){
return null;
}
return new ArrayList<Integer>(parisMap.get(v));
}
public static void readMetro(String fileName) throws Exception, IOException {
read(fileName);
}
public static void read(String fileName) throws Exception, IOException {
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream(fileName),"UTF-8"));
try {
String line = reader.readLine();
String [] lines = line.split(" ");
int totalVertices = Integer.parseInt(lines[0]);
System.out.println("The total number of Vertices " + totalVertices);
int totalEdges = Integer.parseInt(lines[1]);
System.out.println("The total number of Edges " + totalEdges);
line = reader.readLine();
lines = line.split(" ",2);
while( !line.equals("$") ){
System.out.println(" ID: " + lines[0] + " Name: " + lines[1]);
line = reader.readLine();
lines =line.split(" ",2);
verticeList.add(lines[1]);
while(count <= number_edges){
source = scan.nextInt();
dest = scan.nextInt();
adjacencyList.addEdge(source, dest);
count++;
}
}
line = reader.readLine();
while(line != null) {
lines = line.split(" ",3);
System.out.println(" ID1: " + lines[0] + " ID2: " + lines[1] + " Weight: " + lines[2] );
line = reader.readLine();
}
} catch (NumberFormatException e) {System.out.println("FUCKING PROBLEM");}
finally {
reader.close();
}
public static void main(String args[]){
readMetro("metro.txt");
int count = 1, source, dest;
int number_vertices = totalVertices;
int number_edges = totalEdges;
GraphAdjacencyList adjacencyList = new GraphAdjacencyList(number_vertices);
System.out.println("Enter edges in format <source> <destination>");
System.out.println("The given adjacency List for the graph\n");
for(int i=1;i<=number_vertices;i++){
System.out.print(i + "->");
ArrayList<Integer> edgeList = adjacencyList.getNeighbours(i);
for(int j=1;;j++){
if(j!=edgeList.size()){
System.out.print(edgeList.get(j-1)+" -> ");
}
else{
System.out.print(edgeList.get(j-1));
break;
}
}
System.out.println();
}
}
}