-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph2.java
162 lines (127 loc) · 3.95 KB
/
Graph2.java
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
156
157
158
159
160
161
162
/*******************************************************************************
* File: Graph.java
* Author: Andre Berger
*
* Class to represent graphs using adjacency matrix and adjacency lists
******************************************************************************/
import java.util.*;
import java.io.*;
public class Graph {
class Node {
private int id;
private double weight;
public Node(int u, double w) {
this.id = u;
this.weight = w;
}
public int getID() {
return this.id;
}
public double getWeight() {
return this.weight;
}
}
class Edge {
private int firstNode;
private int secondNode;
private double weight;
public Edge(int u, int v, double w) {
this.firstNode = u;
this.secondNode = v;
this.weight = w;
}
public int getFirstNodeOfEdge() {
return this.firstNode;
}
public int getSecondNodeOfEdge() {
return this.secondNode;
}
public double getWeight() {
return this.weight;
}
}
private int numberOfNodes;
private int numberOfEdges;
private int sourceIndex;
private int destIndex;
private ArrayList<Node> nodeList;
private ArrayList<Edge> edgeList;
private int[][] adjacent;
private ArrayList<ArrayList<Edge>> adjacencyLists;
// Constructor for a graph, information is read from file
// adjacency matrix as well as adjacency list is filled
public Graph(String filename) throws java.io.FileNotFoundException {
File file = new File(filename);
Scanner input = new Scanner(file);
this.numberOfNodes = Integer.parseInt(input.nextLine().trim());
this.numberOfEdges = Integer.parseInt(input.nextLine().trim());
this.adjacent = new int[this.numberOfNodes][this.numberOfNodes];
for (int i = 0; i < this.numberOfNodes; i++) {
for (int j = 0; j < this.numberOfNodes; j++) {
adjacent[i][j] = 0;
}
}
this.nodeList = new ArrayList<Node>();
this.edgeList = new ArrayList<Edge>();
this.adjacencyLists = new ArrayList<ArrayList<Edge>>();
for (int i = 0; i < this.numberOfNodes; i++) {
this.adjacencyLists.add(new ArrayList<Edge>());
}
for (int i = 0; i < this.numberOfNodes; i++) {
String line = input.nextLine().trim();
String[] parts = line.split(" ");
int nodeIndex = Integer.parseInt(parts[0].trim());
double w = Double.parseDouble(parts[1].trim());
Node node = new Node(nodeIndex, w);
this.nodeList.add(node);
}
for (int i = 0; i < this.numberOfEdges; i++) {
String line = input.nextLine().trim();
String[] parts = line.split(" ");
int firstNode = Integer.parseInt(parts[0].trim());
int secondNode = Integer.parseInt(parts[1].trim());
double weight = Double.parseDouble(parts[2].trim());
this.adjacent[firstNode][secondNode] = 1;
this.adjacent[secondNode][firstNode] = 1;
Edge edge = new Edge(firstNode, secondNode, weight);
this.edgeList.add(edge);
this.adjacencyLists.get(firstNode).add(edge);
this.adjacencyLists.get(secondNode).add(edge);
}
input.close();
}
public int getSource() {
return this.sourceIndex;
}
public int getDest() {
return this.destIndex;
}
// returns the number of nodes of this graph
public int getNumberOfNodes() {
return this.numberOfNodes;
}
// returns the number of edges of this graph
public int getNumberOfEdges() {
return this.numberOfEdges;
}
// returns whether node i and node j are adjacent in this graph
public boolean isAdjacent(int i, int j) {
return (this.adjacent[i][j] == 1);
}
public ArrayList<Edge> getEdgelist() {
return this.edgeList;
}
public ArrayList<Node> getNodeList() {
return this.nodeList;
}
// returns weight of vertex which is ith in the list
public double getNodeWeight(int i) {
return this.nodeList.get(i).weight;
}
public Edge getEdge(int i) {
return this.edgeList.get(i);
}
public int degree(int i) {
return this.adjacencyLists.get(i).size();
}
}