-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximalMatching.java
145 lines (137 loc) · 4.71 KB
/
MaximalMatching.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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* This algorithm computes a maximal matching for an undirected Graph.
*
* @author Sonja
*/
public class MaximalMatching implements Algorithm {
/**
* Saves the result of the algorithm as a graph.
*/
private Graph result;
/**
* Produces an algorithm for maximal matching with an empty result.
*/
public MaximalMatching() {
result = null;
}
@Override
public ArrayList<Operation> execute(Graph graph) {
if (graph == null) {
throw new NullPointerException();
}
if (graph.isEmpty()) {
throw new IllegalArgumentException();
}
/*
* make copy of the graph to work on (contains E')
* produce container of variables to use later
* produce arraylist of operations that will be done by the algorithm
*/
ArrayList<Integer> vertices = graph.getVertices();
HashMap<Integer, TreeMap<Integer, Integer>> startpoints = graph.getStartpoints();
Integer currentVertex;
Graph copyGraph;
if(graph.typeOfGraph().equals("undirected")) {
copyGraph = new UndirectedGraph();
for(int i = 0; i < vertices.size(); i++) {
currentVertex = vertices.get(i);
copyGraph.addVertex(currentVertex);
}
for(int i = 0; i < vertices.size(); i++) {
currentVertex = vertices.get(i);
TreeMap<Integer, Integer> startAdjacent =graph.getStartpoints().get(currentVertex);
Set<Integer> set = startAdjacent.keySet();
java.util.Iterator<Integer> itr = set.iterator();
while (itr.hasNext()){
copyGraph.addEdge(currentVertex, itr.next());
}
}
}
else {
copyGraph = new DirectedGraph();
}
//Graph copyGraph = graph;
ArrayList<Integer> copyVertices = copyGraph.getVertices();
HashMap<Integer, TreeMap<Integer, Integer>> copyStartpoints = copyGraph.getStartpoints();
//Integer currentVertex;
Integer startVertexName = null;
Integer endVertexName = null;
ArrayList<Operation> changes = new ArrayList<Operation>();
/*
* produce the empty matching (M) as graph without any edges
*/
if(copyGraph.typeOfGraph().equals("undirected")) {
result = new UndirectedGraph();
}
else {
result = new DirectedGraph();
}
for(int i = 0; i < copyVertices.size(); i++) {
currentVertex = copyVertices.get(i);
result.addVertex(currentVertex);
}
/*
* do maximal matching
* return arraylist of operations
*/
while (copyGraph.containsEdges()) {
/*
* find random edge <startVertexName, endVertexName> in copyGraph (<u,v> in E'):
* for all vertices
* if there is an adjacent vertex
* choose the corresponding edge
* delete this edge in the copyGraph (update E')
* memorize this step as an operation
*/
System.out.println("num vertices copy = "+ copyVertices.size());
for(int i = 0; i < copyVertices.size(); i++) {
currentVertex = copyVertices.get(i);
System.out.println("currentVertex = "+ currentVertex);
System.out.println("copyStartpoints.get(currentVertex).firstKey() = "+ copyStartpoints.get(currentVertex));
if (copyStartpoints.containsKey(currentVertex) && !copyStartpoints.get(currentVertex).isEmpty())
{
startVertexName = currentVertex;
endVertexName = copyStartpoints.get(currentVertex).firstKey();
break;
}
}
copyGraph.deleteEdge(startVertexName, endVertexName);
changes.add(new EdgeOperation("consider", startVertexName, endVertexName));
/*
* add this edge to the matching / result to be (update M)
* memorize this step as an operation
*/
result.addEdge(startVertexName, endVertexName);
changes.add(new EdgeOperation("choose", startVertexName, endVertexName));
/*
* delete all edges that are incident:
* for all vertices
* if there is an edge with the start- or the endpoint of the chosen edge
* delete it
*/
for(int i = 0; i < copyVertices.size(); i++) {
currentVertex = copyVertices.get(i);
//System.out.println("currentVertex = "+ currentVertex);
//System.out.println("startVertex = "+ startVertexName);
//System.out.println("endVertex = "+ endVertexName);
if (copyGraph.containsEdge(startVertexName, currentVertex) || copyGraph.containsEdge(currentVertex, startVertexName)) {
copyGraph.deleteEdge(startVertexName, currentVertex);
}
if (copyGraph.containsEdge(endVertexName, currentVertex) || copyGraph.containsEdge(currentVertex, endVertexName)) {
copyGraph.deleteEdge(endVertexName, currentVertex);
}
}
}
return changes;
}
@Override
public Graph getResult(Graph graph){
this.execute(graph);
return result;
}
}