Skip to content

Commit 07f6461

Browse files
committed
Merge pull request #19 from ProgrammingLife2016/TempGraphVisualizer
Temp graph visualizer
2 parents 73debd1 + c8287e0 commit 07f6461

File tree

13 files changed

+534
-280
lines changed

13 files changed

+534
-280
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,4 @@ nbproject/
253253
build/
254254
manifest.mf
255255
build.xml
256+
nbactions.xml

PL2/PL2-core/src/main/java/nl/tudelft/pl2016gr2/core/algorithms/AlgoRunner.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ public static void main(String[] args) {
2929
//MutationAlgorithm m = new MutationAlgorithm();
3030
//m.calc(g);
3131
FindBubbles findBubbles = new FindBubbles(g);
32-
findBubbles.calculateFlows();
33-
FilterInDels filter = new FilterInDels(g);
34-
Graph filteredGraph = filter.filterGraph();
35-
filteredGraph.print();
36-
g.print();
32+
findBubbles.calculateBubbles();
33+
// findBubbles.calculateFlows();
34+
// FilterInDels filter = new FilterInDels(g);
35+
// Graph filteredGraph = filter.filterGraph();
36+
// filteredGraph.print();
37+
//g.print();
3738
//System.out.println(g.getNodes().get(8).getIn().get(0).getParent().getFlow());
3839
long f = System.currentTimeMillis();
3940
System.out.println("The algorithm took " + (f - e) + " milliseconds to run");
Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
1-
package nl.tudelft.pl2016gr2.core.algorithms;
2-
3-
import java.util.ArrayList;
4-
import java.util.HashSet;
5-
import java.util.Set;
6-
7-
import nl.tudelft.pl2016gr2.model.Edge;
8-
import nl.tudelft.pl2016gr2.model.Graph;
9-
import nl.tudelft.pl2016gr2.model.Node;
10-
11-
public class FilterInDels {
12-
13-
private Graph graph;
14-
/**
15-
* Contains all nodes that are part of an indel
16-
*/
17-
private Set<Integer> usedNodes = new HashSet<>();
18-
19-
public FilterInDels(Graph graph) {
20-
this.graph = graph;
21-
}
22-
23-
public Graph filterGraph() {
24-
ArrayList<Node> root = new ArrayList<>();
25-
ArrayList<Node> nodes = graph.getNodes();
26-
root.add(nodes.get(0));
27-
root.add(nodes.get(1));
28-
Graph filteredGraph = new Graph(root);
29-
30-
for (int i = 0; i < nodes.size(); i++) {
31-
Node source = nodes.get(i);
32-
33-
if (!usedNodes.contains(source.getId())) {
34-
Node[] res = getInsertion(source);
35-
if (res != null) {
36-
Node insertion = res[0];
37-
Node end = res[1];
38-
39-
usedNodes.add(insertion.getId());
40-
Node inDel = new Node(source.getId());
41-
inDel.setInDel(true);
42-
inDel.setBubbleStart(source.getId());
43-
inDel.setBubbleEnd(end.getId());
44-
45-
for (Edge edge : source.getIn()) {
46-
inDel.addIn(edge);
47-
}
48-
49-
for (Edge edge : end.getOut()) {
50-
inDel.addOut(edge);
51-
}
52-
53-
filteredGraph.addNode(inDel);
54-
} else {
55-
filteredGraph.addNode(source);
56-
}
57-
}
58-
}
59-
60-
return filteredGraph;
61-
}
62-
63-
private Node[] getInsertion(Node source) {
64-
ArrayList<Edge> outEdges = source.getOut();
65-
66-
if (outEdges.size() != 2) {
67-
return null;
68-
}
69-
70-
Node target1 = outEdges.get(0).getTarget();
71-
Node target2 = outEdges.get(1).getTarget();
72-
73-
// First one is the insertion, second one is the initial endpoint
74-
if (target1.getOut().size() != 0 && target1.getOut().get(0).getTarget().equals(target2) && target1.getIn().size() == 1 && target2.getIn().size() == 2) {
75-
Node[] res = {target1, target2};
76-
return res;
77-
} else if(target2.getOut().size() != 0 && target2.getOut().get(0).getTarget().equals(target1) && target2.getIn().size() == 1 && target1.getIn().size() == 2) {
78-
Node[] res = {target2, target1};
79-
return res;
80-
}
81-
82-
return null;
83-
}
84-
}
1+
//package nl.tudelft.pl2016gr2.core.algorithms;
2+
//
3+
//import java.util.ArrayList;
4+
//import java.util.HashSet;
5+
//import java.util.Set;
6+
//
7+
//import nl.tudelft.pl2016gr2.model.Edge;
8+
//import nl.tudelft.pl2016gr2.model.Graph;
9+
//import nl.tudelft.pl2016gr2.model.Node;
10+
//
11+
//public class FilterInDels {
12+
//
13+
// private Graph graph;
14+
// /**
15+
// * Contains all nodes that are part of an indel
16+
// */
17+
// private Set<Integer> usedNodes = new HashSet<>();
18+
//
19+
// public FilterInDels(Graph graph) {
20+
// this.graph = graph;
21+
// }
22+
//
23+
// public Graph filterGraph() {
24+
// ArrayList<Node> root = new ArrayList<>();
25+
// ArrayList<Node> nodes = graph.getNodes();
26+
// root.add(nodes.get(0));
27+
// root.add(nodes.get(1));
28+
// Graph filteredGraph = new Graph(root);
29+
//
30+
// for (int i = 0; i < nodes.size(); i++) {
31+
// Node source = nodes.get(i);
32+
//
33+
// if (!usedNodes.contains(source.getId())) {
34+
// Node[] res = getInsertion(source);
35+
// if (res != null) {
36+
// Node insertion = res[0];
37+
// Node end = res[1];
38+
//
39+
// usedNodes.add(insertion.getId());
40+
// Node inDel = new Node(source.getId());
41+
// inDel.setInDel(true);
42+
// inDel.setBubbleStart(source.getId());
43+
// inDel.setBubbleEnd(end.getId());
44+
//
45+
// for (Edge edge : source.getIn()) {
46+
// inDel.addIn(edge);
47+
// }
48+
//
49+
// for (Edge edge : end.getOut()) {
50+
// inDel.addOut(edge);
51+
// }
52+
//
53+
// filteredGraph.addNode(inDel);
54+
// } else {
55+
// filteredGraph.addNode(source);
56+
// }
57+
// }
58+
// }
59+
//
60+
// return filteredGraph;
61+
// }
62+
//
63+
// private Node[] getInsertion(Node source) {
64+
// ArrayList<Edge> outEdges = source.getOut();
65+
//
66+
// if (outEdges.size() != 2) {
67+
// return null;
68+
// }
69+
//
70+
// Node target1 = outEdges.get(0).getTarget();
71+
// Node target2 = outEdges.get(1).getTarget();
72+
//
73+
// // First one is the insertion, second one is the initial endpoint
74+
// if (target1.getOut().size() != 0 && target1.getOut().get(0).getTarget().equals(target2) && target1.getIn().size() == 1 && target2.getIn().size() == 2) {
75+
// Node[] res = {target1, target2};
76+
// return res;
77+
// } else if(target2.getOut().size() != 0 && target2.getOut().get(0).getTarget().equals(target1) && target2.getIn().size() == 1 && target1.getIn().size() == 2) {
78+
// Node[] res = {target2, target1};
79+
// return res;
80+
// }
81+
//
82+
// return null;
83+
// }
84+
//}

PL2/PL2-core/src/main/java/nl/tudelft/pl2016gr2/core/algorithms/FindBubbles.java

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,142 @@
11
package nl.tudelft.pl2016gr2.core.algorithms;
22

33
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.PriorityQueue;
9+
import java.util.Queue;
10+
import java.util.Set;
411

5-
import nl.tudelft.pl2016gr2.model.Edge;
12+
import nl.tudelft.pl2016gr2.model.Bubble;
613
import nl.tudelft.pl2016gr2.model.Graph;
714
import nl.tudelft.pl2016gr2.model.Node;
815

916
public class FindBubbles {
1017

1118
private Graph graph;
19+
private HashMap<Integer, PriorityQueue<Bubble>> bubbles = new HashMap<>();;
20+
private Set<Bubble> collapsedBubbles = new HashSet<>();
21+
private PriorityQueue<Node> flows;
1222

1323
public FindBubbles(Graph graph) {
1424
this.graph = graph;
25+
26+
flows = new PriorityQueue<>((Node node1, Node node2) -> {
27+
int comparison = Double.compare(node1.getFlow(), node2.getFlow());
28+
29+
if (comparison == 0) {
30+
return Integer.compare(node1.getId(), node2.getId());
31+
}
32+
33+
return comparison;
34+
});
1535
}
1636

17-
public void calculateFlows() {
37+
public Graph calculateBubbles() {
38+
Graph overview = new Graph(new ArrayList<Bubble>());
39+
40+
init();
1841
sendFlow();
42+
43+
while(!flows.isEmpty()) {
44+
Node curNode = flows.poll();
45+
//System.out.println(curNode);
46+
47+
Node nextNode = flows.peek();
48+
49+
if (curNode.getFlow() == nextNode.getFlow()) {
50+
// is bubble
51+
Bubble bubble = new Bubble(curNode.getId());
52+
bubble.setInLinks(curNode.getInLinks());
53+
bubble.setOutLinks(nextNode.getOutLinks());
54+
bubble.setStartBubble(curNode);
55+
bubble.setEndBubble(nextNode);
56+
findNestedBubbles(curNode, nextNode, bubble);
57+
bubbles.get(bubble.getId()).offer(bubble);
58+
}
59+
}
60+
61+
for (PriorityQueue<Bubble> bubbleLevels : bubbles.values()) {
62+
overview.addNode(bubbleLevels.peek());
63+
}
64+
65+
return overview;
66+
}
67+
68+
private void findNestedBubbles(Bubble startBubble, Bubble endBubble, Bubble newBubble) {
69+
if (startBubble.getOutLinks().size() == 1 && startBubble.getOutLinks().get(0).equals(endBubble)) {
70+
return;
71+
}
72+
73+
Set<Integer> visited = new HashSet<>();
74+
visited.add(endBubble.getId());
75+
76+
Queue<Integer> toVisit = new LinkedList<>();
77+
toVisit.addAll(startBubble.getOutLinks());
78+
79+
int highestLevel = 0;
80+
81+
while (!toVisit.isEmpty()) {
82+
Bubble bubble = bubbles.get(toVisit.poll()).peek();
83+
84+
if (!bubble.equals(endBubble)) {
85+
newBubble.addNestedBubble(bubble.getId());
86+
visited.add(bubble.getId());
87+
88+
highestLevel = bubble.getLevel() > highestLevel ? bubble.getLevel() : highestLevel;
89+
90+
for (Integer target : bubble.getOutLinks()) {
91+
if (!visited.contains(target)) {
92+
toVisit.add(target);
93+
}
94+
}
95+
}
96+
}
97+
98+
newBubble.setLevel(highestLevel);
1999
}
20100

101+
private void init() {
102+
// Puts the bubble with the highest level first
103+
Comparator<Bubble> compareLevels = (Bubble bubble1, Bubble bubble2) -> {
104+
return Integer.compare(bubble2.getLevel(), bubble1.getLevel());
105+
};
106+
107+
for (Bubble node : graph.getNodes()) {
108+
PriorityQueue<Bubble> levels = new PriorityQueue<>(compareLevels);
109+
levels.add(node);
110+
bubbles.put(node.getId(), levels);
111+
}
112+
}
113+
21114
private void sendFlow() {
22115
double flowStart = (double)graph.getSize();
23-
graph.getRoot().setFlow(flowStart);
116+
//graph.getRoot().setFlow(flowStart);
117+
Node root = (Node) graph.getRoot();
118+
root.setFlow(flowStart);
24119

25-
for (Node node : graph.getNodes()) {
26-
System.out.println(node);
27-
ArrayList<Edge> outEdges = node.getOut();
120+
for (Bubble bubble : graph.getNodes()) {
121+
Node node = (Node) bubble;
122+
flows.offer(node);
123+
124+
// System.out.println(node);
125+
ArrayList<Integer> outLinks = node.getOutLinks();
28126
double remainingFlow = node.getFlow();
29127

30-
for (int i = 0; i < outEdges.size(); i++) {
31-
Node target = outEdges.get(i).getTarget();
128+
for (int i = 0; i < outLinks.size(); i++) {
129+
Node target = (Node) bubbles.get(outLinks.get(i)).peek();
32130

33-
if (i == node.getOut().size() - 1) {
131+
if (i == outLinks.size() - 1) {
34132
target.addFlow(remainingFlow);
35133
} else {
36134
double random = Math.random();
135+
if (random == 0) {
136+
random += 0.1;
137+
} else if (random == 1) {
138+
random -= 0.1;
139+
}
37140
double flowToAdd = random * remainingFlow;
38141
remainingFlow -= flowToAdd;
39142
target.addFlow(flowToAdd);

PL2/PL2-core/src/main/java/nl/tudelft/pl2016gr2/core/algorithms/MutationAlgorithm.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package nl.tudelft.pl2016gr2.core.algorithms;
22

3-
import java.util.ArrayList;
4-
import java.util.LinkedList;
5-
import java.util.Queue;
6-
7-
import nl.tudelft.pl2016gr2.model.Edge;
8-
import nl.tudelft.pl2016gr2.model.Graph;
9-
import nl.tudelft.pl2016gr2.model.Node;
103

114

125
public class MutationAlgorithm {

0 commit comments

Comments
 (0)