-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_graph.java
More file actions
91 lines (83 loc) · 2.25 KB
/
p_graph.java
File metadata and controls
91 lines (83 loc) · 2.25 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
// This code contains the following legitimate syntax usages
// - Method calls with no arguments
// - A class with some members
// - Multi-dimensional array declaration, creation, and usage
// - Complicated boolean expression with comparison and logical operation
// - '.length' opearation on different levels of 2D array
class p_GraphDemo {
public static void main(String []a) { {
System.out.println(new GraphTester().testGraph());
} }
}
class GraphTester {
Graph g;
public int testGraph(){
int ret;
g = new Graph();
ret = g.create(5, false);
ret = g.addEdge(0, 1);
ret = g.addEdge(0, 2);
ret = g.addEdge(0, 3);
ret = g.addEdge(0, 4);
if(g.edgeExists(0, 1)){
System.out.println("0, 1");
}else {}
if(g.edgeExists(1, 0)){
System.out.println("0, 1");
}else {}
if(g.edgeExists(4, 0)){
System.out.println("4, 0");
}else {}
if(g.edgeExists(3,2)){
System.out.println("3, 2");
}else {}
return ret;
}
}
class Graph {
boolean[][] adjacency;
public int create(int nNodes, boolean reflexive) {
adjacency = new boolean[nNodes][nNodes];
return 0;
}
public int addEdge(int u, int v) {
int errorCode;
if (u >= adjacency.length || v >= adjacency.length || u < 0 || v < 0) {
errorCode = -1;
} else {
adjacency[u][v] = true;
adjacency[v][u] = true;
errorCode = 0;
}
return errorCode;
}
public int removeEdge(int u, int v) {
int errorCode;
errorCode = 0;
if (u >= 0 && v >= 0 && u < adjacency.length && v < adjacency.length) {
adjacency[u][v] = false;
adjacency[v][u] = false;
errorCode = 0;
} else {
errorCode = -1;
}
return errorCode;
}
public int [] connectedComponent() {
boolean[] visited;
int []color;
if (adjacency.length == adjacency[0].length) {
color = new int[adjacency.length];
visited = new boolean[adjacency.length];
/* BFS or DFS, but not complete yet*/
} else { color = new int[1];}
return color;
}
public boolean edgeExists(int x, int y){
return adjacency[x][y];
}
}
class MultiGraph extends Graph {
// edge between 2 nodes can have some multiplicity.
int[][] multiplicity;
}