-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
48 lines (37 loc) · 842 Bytes
/
Graph.java
File metadata and controls
48 lines (37 loc) · 842 Bytes
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
public class Graph {
int numNodes;
Node[] nodes;
//adjacency matrix
int[][] adjMatrix;
public void addEdge(int i, int j) {
if(i >= 0 && j >= 0 && i < numNodes && j < numNodes)
{
//drawing an edge from both nodes
adjMatrix[i][j] = 1;
adjMatrix[j][i] = 1;
}
return;
}
//returns true if there is an edge between two vertices
public boolean edgeExists(int i, int j)
{
if(i >= 0 && j >= 0 && i < numNodes && j < numNodes)
{
if (adjMatrix[i][j] == 1 && adjMatrix[j][i] == 1)
{
return true;
}
}
return false;
}
public Graph(int num) {
numNodes = num;
nodes = new Node[numNodes];
for(int i = 0; i < numNodes; i++) {
nodes[i] = new Node(i);
}
// you might also want to do other things here
//creating the adj. matrix
adjMatrix = new int[numNodes][numNodes];
}
}