Skip to content

Commit 3542b5a

Browse files
author
Your Name
committed
Merge branch 'intervalDecomposition' of https://github.com/tj-developers/jgrapht into intervalDecompositionDia
2 parents f014e21 + f8faedf commit 3542b5a

File tree

8 files changed

+878
-52
lines changed

8 files changed

+878
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package org.jgrapht.alg.decomposition;
2+
3+
import java.util.*;
4+
import org.jgrapht.*;
5+
import org.jgrapht.alg.intervalgraph.IntervalGraphRecognizer;
6+
import org.jgrapht.intervalgraph.*;
7+
import org.jgrapht.intervalgraph.interval.*;
8+
9+
/**
10+
* Class for calculating the nice Tree Decomposition for interval graphs
11+
*
12+
* @param <T> the value type of the intervals of the interval graph
13+
* @param <V> the type of the nodes of the input graph
14+
*
15+
* @author Ira Justus Fesefeldt (PhoenixIra)
16+
* @since Mai 14, 2018
17+
*/
18+
public class IntervalGraphNiceDecompositionBuilder<T extends Comparable<T>, V> extends NiceDecompositionBuilder<V>
19+
{
20+
// input to the algorithm, list of sorted intervals
21+
private List<Interval<T>> startSort, endSort;
22+
private Map<Interval<T>, V> intervalToVertexMap;
23+
private Map<V, Interval<T>> vertexToIntervalMap;
24+
25+
// helper attributes
26+
private Integer currentVertex = null;
27+
28+
/**
29+
* Private constructor for the factory methods, which changes the inputs
30+
*
31+
* @param sortedByStartPoint the intervals sorted after the starting points
32+
* @param sortedByEndPoint the intervals sorted after the ending points
33+
* @param intervalToVertexMap maps intervals to the vertices of the graph (may be the same)
34+
*/
35+
private IntervalGraphNiceDecompositionBuilder(
36+
List<Interval<T>> sortedByStartPoint, List<Interval<T>> sortedByEndPoint,
37+
Map<Interval<T>, V> intervalToVertexMap, Map<V, Interval<T>> vertexToIntervalMap)
38+
{
39+
super();
40+
41+
this.startSort = sortedByStartPoint;
42+
this.endSort = sortedByEndPoint;
43+
this.intervalToVertexMap = intervalToVertexMap;
44+
this.vertexToIntervalMap = vertexToIntervalMap;
45+
46+
computeNiceDecomposition();
47+
}
48+
49+
/**
50+
* Factory method for creating a nice tree decomposition for interval graphs. This factory
51+
* method uses general graphs and the IntervalGraphRecognizer to generate a list of intervals
52+
* sorted by starting and ending points. The complexity of this method depends on the sorting
53+
* algorithm in IntervalGraphRecognizer (probably quasi linear in the number of intervals)
54+
*
55+
* @param graph the graph which should transformed to an interval graph and then into a
56+
* corresponding nice tree decomposition
57+
* @param <V> the vertex type of the graph
58+
* @param <E> the edge type of the graph
59+
* @return the algorithm for the computation of the nice tree decomposition, returns null if
60+
* graph was no interval graph
61+
* @see IntervalGraphRecognizer
62+
*/
63+
public static <V, E> IntervalGraphNiceDecompositionBuilder<Integer, V> create(Graph<V, E> graph)
64+
{
65+
IntervalGraphRecognizer<V, E> recog = new IntervalGraphRecognizer<>(graph);
66+
67+
HashMap<V, Interval<Integer>> vertexToIntegerMap =
68+
new HashMap<>(recog.getVertexToIntervalMap().size());
69+
70+
Map<V,IntervalVertexInterface<V, Integer>> vertexToIntervalVertexMap = recog.getVertexToIntervalMap();
71+
for (V key : vertexToIntervalVertexMap.keySet())
72+
vertexToIntegerMap.put(key, vertexToIntervalVertexMap.get(key).getInterval());
73+
74+
if (recog.isIntervalGraph())
75+
return new IntervalGraphNiceDecompositionBuilder<Integer, V>(
76+
recog.getIntervalsSortedByStartingPoint(),
77+
recog.getIntervalsSortedByEndingPoint(), recog.getIntervalToVertexMap(),
78+
vertexToIntegerMap);
79+
else
80+
return null;
81+
}
82+
83+
/**
84+
* Factory method for creating a nice tree decomposition for interval graphs. This factory
85+
* method extracts the intervals from the interval graph and uses them as an input for the
86+
* computation. The complexity of this method depends on the sorting algorithm of ArrayList
87+
* (probably O(|V| log(|V|))
88+
*
89+
* @param intervalGraph the input for which a nice tree decomposition should be computed
90+
* @param <V> the IntervalVertex Type of the interval graph
91+
* @param <E> the edge type of the interval graph
92+
* @param <VertexType> the vertex type of the graph
93+
* @param <T> the value of the intervals
94+
* @return the algorithm for the computation of the nice tree decomposition
95+
* @see ArrayList#sort(Comparator)
96+
*/
97+
public static <V extends IntervalVertexInterface<VertexType, T>, E, VertexType,
98+
T extends Comparable<T>> IntervalGraphNiceDecompositionBuilder<T, V> create(
99+
IntervalGraph<V, E, VertexType, T> intervalGraph)
100+
{
101+
Set<V> vertexSet = intervalGraph.vertexSet();
102+
List<Interval<T>> intervals = new ArrayList<Interval<T>>(vertexSet.size());
103+
Map<Interval<T>, V> intervalToVertexMap = new HashMap<>(vertexSet.size());
104+
Map<V, Interval<T>> vertexToIntervalMap = new HashMap<>(vertexSet.size());
105+
for (V iv : vertexSet) {
106+
intervals.add(iv.getInterval());
107+
intervalToVertexMap.put(iv.getInterval(), iv);
108+
vertexToIntervalMap.put(iv, iv.getInterval());
109+
}
110+
ArrayList<Interval<T>> startSort = new ArrayList<>(intervals);
111+
ArrayList<Interval<T>> endSort = new ArrayList<>(intervals);
112+
startSort.sort(Interval.<T> getStartingComparator());
113+
endSort.sort(Interval.<T> getEndingComparator());
114+
return new IntervalGraphNiceDecompositionBuilder<T, V>(
115+
startSort, endSort, intervalToVertexMap, vertexToIntervalMap);
116+
}
117+
118+
/**
119+
* Factory method for creating a nice tree decomposition for interval graphs. This factory
120+
* method needs to lists of intervals, the first sorted after starting points, the second after
121+
* ending points The complexity of this method is linear in the number of intervals
122+
*
123+
* @param sortedByStartPoint a list of all intervals sorted by the starting point
124+
* @param sortedByEndPoint a list of all intervals sorted by the ending point
125+
* @param <T> the value of the intervals
126+
* @return the algorithm for the computation of the nice tree decomposition
127+
*/
128+
public static <T extends Comparable<T>> IntervalGraphNiceDecompositionBuilder<T, Interval<T>> create(
129+
List<Interval<T>> sortedByStartPoint, List<Interval<T>> sortedByEndPoint)
130+
{
131+
HashMap<Interval<T>, Interval<T>> identity = new HashMap<>(sortedByStartPoint.size());
132+
for (Interval<T> interval : sortedByStartPoint)
133+
identity.put(interval, interval);
134+
return new IntervalGraphNiceDecompositionBuilder<T, Interval<T>>(
135+
new ArrayList<Interval<T>>(sortedByStartPoint),
136+
new ArrayList<Interval<T>>(sortedByEndPoint), identity, identity);
137+
}
138+
139+
/**
140+
* Factory method for creating a nice tree decomposition for interval graphs. This factory
141+
* method needs to lists of intervals, which then is sorted by ArrayList.sort(). The complexity
142+
* of this method depends on the sorting Algorithm of ArrayList (probably O(|List| log(|List|))
143+
*
144+
* @param intervals the (unsorted) list of all intervals
145+
* @param <T> the values of the intervals
146+
* @return the algorithm for the computation of the nice tree decomposition
147+
* @see ArrayList#sort(Comparator)
148+
*/
149+
public static <T extends Comparable<T>> IntervalGraphNiceDecompositionBuilder<T, Interval<T>> create(
150+
List<Interval<T>> intervals)
151+
{
152+
ArrayList<Interval<T>> startSort = new ArrayList<>(intervals);
153+
ArrayList<Interval<T>> endSort = new ArrayList<>(intervals);
154+
startSort.sort(Interval.<T> getStartingComparator());
155+
endSort.sort(Interval.<T> getEndingComparator());
156+
return create(startSort, endSort);
157+
}
158+
159+
/**
160+
* Main method for computing the nice tree decomposition
161+
*/
162+
private void computeNiceDecomposition()
163+
{
164+
165+
// create all objects and set new root
166+
currentVertex = getRoot();
167+
168+
int endIndex = 0;
169+
170+
// as long as intervals remain
171+
for (Interval<T> current : startSort) {
172+
// first forget until you need to introduce new nodes
173+
while (endSort.get(endIndex).getEnd().compareTo(current.getStart()) < 0) {
174+
V forgetElement = intervalToVertexMap.get(endSort.get(endIndex));
175+
currentVertex = addForget(forgetElement, currentVertex);
176+
endIndex++;
177+
}
178+
V introduceElement = intervalToVertexMap.get(current);
179+
currentVertex = addIntroduce(introduceElement, currentVertex);
180+
}
181+
// add the last forget nodes
182+
while (endIndex < endSort.size()) {
183+
V forgetElement = intervalToVertexMap.get(endSort.get(endIndex++));
184+
currentVertex = addForget(forgetElement, currentVertex);
185+
}
186+
}
187+
188+
/**
189+
* getter for interval to vertex Map
190+
*
191+
* @return a map that maps intervals to vertices
192+
*/
193+
public Map<Interval<T>, V> getIntervalToVertexMap()
194+
{
195+
return intervalToVertexMap;
196+
}
197+
198+
/**
199+
* getter for vertex to interval map
200+
*
201+
* @return a map that maps vertices to intervals
202+
*/
203+
public Map<V, Interval<T>> getVertexToIntervalMap()
204+
{
205+
return vertexToIntervalMap;
206+
}
207+
}

0 commit comments

Comments
 (0)