-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
154 lines (138 loc) · 3.96 KB
/
Graph.java
File metadata and controls
154 lines (138 loc) · 3.96 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
import java.util.*;
class Node{
private int id;
private ArrayList<Node> connections = new ArrayList<>();
Node(int id){
this.id = id;
}
int getId(){
return id;
}
ArrayList<Node> getConnections(){
return connections;
}
void addConnections(Node n){
connections.add(n);
}
int getDegree(){
return connections.size();
}
void removeConnection(Node n){
connections.remove(n);
}
public String toString(){
return String.valueOf(id);
}
}
public class Graph {
ArrayList<Node> nodes = new ArrayList<>();
public int startVertex = 0;
public Graph(int[][] adjMat){
int size = adjMat.length;
for (int i=0; i<size; i++){
nodes.add(new Node(i));
}
for (int i=0; i<size; i++){
int rowCount = adjMat[i].length;
// Filter non square matrices
if (rowCount != size){
nodes = new ArrayList<>(Arrays.asList(new Node[]{}));
return;
}
for (int j=0; j<rowCount; j++){
if (adjMat[i][j] == 1){
// undirected edge
nodes.get(i).addConnections(nodes.get(j));
}
}
}
}
public boolean checkIfEvenDegrees(){
for (Node n:nodes){
if (n.getDegree()%2!=0){
return false;
}
}
return true;
}
private int DFS(Node startNode){
ArrayList<Node> visited = new ArrayList<>();
LinkedList<Node> stack = new LinkedList<>();
int count = 0;
stack.push(startNode);
visited.add(startNode);
while (stack.size() != 0){
Node curSearch = stack.pop();
count++;
for (Node av: curSearch.getConnections()){
if(!visited.contains(av)){
stack.push(av);
visited.add(av);
}
}
}
return count;
}
private boolean isBridge(Node n1, Node n2){
int count1 = DFS(n2);
n1.removeConnection(n2);
n2.removeConnection(n1);
int count2 = DFS(n2);
n1.addConnections(n2);
n2.addConnections(n1);
return (count1>count2) ? true:false;
}
private boolean isValidNextEdge(Node n1, Node n2){
if (n1.getDegree() == 1){
return true;
}
if (!isBridge(n1,n2)){
return true;
}
return false;
}
private void printEuler(Node n){
ArrayList<Node> adjNodes = n.getConnections();
for (int i=0; i<adjNodes.size(); i++){
Node adj = adjNodes.get(i);
if (isValidNextEdge(n, adj)){
System.out.println(n + " -> " + adj);
n.removeConnection(adj);
adj.removeConnection(n);
printEuler(adj);
break;
}
}
}
// see if you can save and restore the graph
public void getEulerPath(){
if (!checkIfEvenDegrees()){
System.out.println("Graph cannot have Euler path as it has one or more vertices of odd degree");
return;
}
if (nodes.size() == 0){
return;
}
printEuler(nodes.get(startVertex));
}
public String toString(){
String returnString = "";
for (int i=0; i<nodes.size(); i++){
Node curNode = nodes.get(i);
var conns = curNode.getConnections();
if (conns.size() == 0){
returnString += curNode + "";
}
for (int j=0;j<conns.size();j++){
returnString += curNode + " <-> " + conns.get(j);
if (j != conns.size()-1){
returnString += ", ";
}
}
if (i != nodes.size()-1){
returnString += "\n";
}
}
return returnString;
}
}