Skip to content

Commit

Permalink
add edge coloring (incorrect...)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxarndt committed May 10, 2017
1 parent 730414e commit f86acf6
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 9 deletions.
104 changes: 102 additions & 2 deletions src/main/java/de/fhws/fiw/mis/graph/color/ColorAlgorithm.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.fhws.fiw.mis.graph.color;

import de.fhws.fiw.mis.graph.AbstractGraph;
import de.fhws.fiw.mis.graph.*;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Arrays;
import java.util.*;
import java.util.stream.Collectors;

/**
* Created by maxarndt on 27.04.17.
Expand All @@ -24,6 +26,104 @@ public static void greedyCol(AbstractGraph graph) {
}

public static void colorEdges(AbstractGraph graph) {
if(graph instanceof DirectedBaseGraph) throw new NotImplementedException();

if(graph.getAllEdges().size() == 0) {
return;
}
else {
Edge edge = graph.getAllEdges().stream().findFirst().get();
AbstractGraph clone = new UndirectedBaseGraph((UndirectedBaseGraph)graph);
clone.removeEdge(edge);
colorEdges(clone);
if(clone.getDegree() < graph.getDegree()) {
edge.setColor(Arrays.asList(HtmlColor.values()).stream()
.filter(c -> graph.getEdgesOf(edge.getSource()).stream()
.noneMatch(x -> x.getColor().getColor().equals(c))
&& graph.getEdgesOf(edge.getTarget()).stream()
.noneMatch(x -> x.getColor().getColor().equals(c)))
.findFirst().get());
}
else {
HtmlColor col = lemma2_10(clone, edge.getSource(), edge.getTarget());
if(col != null)
edge.setColor(col);
}
}
}
public static HtmlColor lemma2_10(AbstractGraph graph, Vertex u, Vertex v) {
int i = 0;
HtmlColor c0 = Arrays.asList(HtmlColor.values()).stream()
.filter(c -> graph.getEdgesOf(v).stream()
.noneMatch(x -> x.getColor().getColor().equals(c)))
.findFirst().get();

List<Edge> edgesOfU = graph.getEdgesOf(u).stream().collect(Collectors.toList());

while(edgesOfU.size() > i && edgesOfU.get(i).getColor().getColor().ordinal() == i) {
final int j = i;
HtmlColor missingColor = getFirstMissingColor(graph.getEdgesOf(edgesOfU.get(i).getTarget().equals(u) ? edgesOfU.get(i).getSource() : edgesOfU.get(i).getTarget()));

graph.getAllEdges().stream().filter(e -> e.getColor().getColor().ordinal() == j + 1)
.forEach(x -> x.setColor(missingColor));

i++;
}
return c0;
}

public static HtmlColor lemma210(AbstractGraph graph, Vertex u, Vertex v) {
int i = 0;
List<HtmlColor> colorList = Arrays.asList(Arrays.stream(HtmlColor.values()) //hält an den v_i fehlenden Farben. An unprocessedNeighbors[0] fehlt colorList[1]
.filter(c -> graph.getEdgesOf(v).stream()
.noneMatch(x -> x.getColor().getColor().equals(c)))
.findFirst().get());

List<Edge> unprocessedNeighbors = graph.getEdgesOf(u).stream().filter(e -> e.getTarget().equals(v) || e.getSource().equals(v)).collect(Collectors.toList());
List<Edge> processedNeighbors = new ArrayList<>();



//a
//wenn an v_i eine Farbe fehlt, die an u fehlt, bis zu diesem Knoten alle Farben umfärben und die fehlende Farbe genau für diese Kante nutzen.


while(true) {
Optional<Edge> e = unprocessedNeighbors.stream().filter(x -> x.getColor().getColor().equals(colorList.get(i))).findFirst();
if(!e.isPresent()) {
//rollback algo
break;
}
else if(processedNeighbors.contains(e.get())) {
//prblemkante entfernen und dann rollback
//+ richtige Farbe finden für Problemkante
break;
}
else {


Vertex v_i = e.get().getSource().equals(u) ? e.get().getTarget() : e.get().getSource();
HtmlColor missingColor = Arrays.stream(HtmlColor.values())
.filter(c -> graph.getEdgesOf(v_i).stream()
.noneMatch(x -> x.getColor().getColor().equals(c)))
.findFirst().get();
colorList.add(missingColor);
processedNeighbors.add(e.get());
}


}

return colorList.get(0);
}

public static boolean isAnyEdgeSpecificColored(List<Edge> edges, int htmlColorIndex) {
return edges.stream().anyMatch(c -> c.getColor().getColor().ordinal() == htmlColorIndex);
}
public static HtmlColor getFirstMissingColor(Set<Edge> edges) {
return Arrays.asList(HtmlColor.values()).stream()
.filter(c -> edges.stream()
.noneMatch(x -> x.getColor().getColor().equals(c)))
.findFirst().get();
}
}
4 changes: 4 additions & 0 deletions src/main/java/de/fhws/fiw/mis/webapp/bean/IndexBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public void greedyCol() {
ColorAlgorithm.greedyCol(graph);
updateGraph();
}
public void colorEdges() {
ColorAlgorithm.colorEdges(graph);
updateGraph();
}


private void updateGraph() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@
<f:ajax execute="btn-greedyCol" render="beanJS" />
</h:commandButton>
</div>
<div class="btn-group" role="group">
<h:commandButton id="btn-colorEdges" value="Color Edges" action="#{indexBean.colorEdges}" type="button" class="btn btn-default">
<f:ajax execute="btn-colorEdges" render="beanJS" />
</h:commandButton>
</div>
</div>
</div>
</div>
Expand Down
89 changes: 82 additions & 7 deletions src/test/java/de/fhws/fiw/mis/graph/color/ColorAlgorithmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.*;

/**
Expand All @@ -29,15 +31,88 @@ public void setUp() {
g.addEdge("B", "D");
g.addEdge("C", "D");
}
public void setUp5VerticeGraph() {
g.addVertex(new VertexBase("E"));
g.addEdge("A", "E");
g.addEdge("B", "E");
g.addEdge("C", "E");
g.addEdge("D", "E");
}
public void setUp6VerticeGraph() {
setUp5VerticeGraph();
g.addVertex(new VertexBase("F"));
g.addEdge("A", "F");
g.addEdge("B", "F");
g.addEdge("C", "F");
g.addEdge("D", "F");
g.addEdge("E", "F");
}

@Test
public void testColorEdgesAB() {
ColorAlgorithm.colorEdges(g);
assertEquals(HtmlColor.values()[2], g.getEdges("A", "B").stream().findFirst().get().getColor().getColor());
}

@Test
public void testColorEdgesAC() {
ColorAlgorithm.colorEdges(g);
assertEquals(HtmlColor.values()[0], g.getEdges("A", "C").stream().findFirst().get().getColor().getColor());
}

@Test
public void testColorEdgesAD() {
ColorAlgorithm.colorEdges(g);
assertEquals(HtmlColor.values()[1], g.getEdges("A", "D").stream().findFirst().get().getColor().getColor());
}

@Test
public void testColorEdges() {
public void testColorEdgesBC() {
ColorAlgorithm.colorEdges(g);
assertEquals(HtmlColor.values()[0], g.getEdges("A", "B").stream().findFirst().get().getColor().getColor());
assertEquals(HtmlColor.values()[1], g.getEdges("A", "C").stream().findFirst().get().getColor().getColor());
assertEquals(HtmlColor.values()[2], g.getEdges("A", "D").stream().findFirst().get().getColor().getColor());
assertEquals(HtmlColor.values()[2], g.getEdges("B", "C").stream().findFirst().get().getColor().getColor());
assertEquals(HtmlColor.values()[1], g.getEdges("B", "D").stream().findFirst().get().getColor().getColor());
assertEquals(HtmlColor.values()[0], g.getEdges("C", "D").stream().findFirst().get().getColor().getColor());
assertEquals(HtmlColor.values()[1], g.getEdges("B", "C").stream().findFirst().get().getColor().getColor());
}

@Test
public void testColorEdgesBD() {
ColorAlgorithm.colorEdges(g);
assertEquals(HtmlColor.values()[0], g.getEdges("B", "D").stream().findFirst().get().getColor().getColor());
}

@Test
public void testColorEdgesCD() {
ColorAlgorithm.colorEdges(g);
assertEquals(HtmlColor.values()[2], g.getEdges("C", "D").stream().findFirst().get().getColor().getColor());
}

@Test
public void testColorEdges5er() {
setUp5VerticeGraph();
ColorAlgorithm.colorEdges(g);
assertFalse(true);
}

@Test
public void testColorEdges6er() {
setUp6VerticeGraph();
ColorAlgorithm.colorEdges(g);
assertFalse(true);
}

@Test
public void testIsAnyEdgeSpecificColored() {
g.getEdges("A", "B").stream().findFirst().get().setColor(HtmlColor.BLUE);
assertTrue(ColorAlgorithm.isAnyEdgeSpecificColored(new ArrayList<>(g.getAllEdges()), 4));
}

@Test
public void testGetFirstMissingColor() {
g.getEdges("A", "B").stream().findFirst().get().setColor(HtmlColor.BLUE);
assertEquals(HtmlColor.AQUA, ColorAlgorithm.getFirstMissingColor(g.getAllEdges()));
}
@Test
public void testGetFirstMissingColor2() {
g.getEdges("A", "B").stream().findFirst().get().setColor(HtmlColor.BLUE);
g.getEdges("A", "C").stream().findFirst().get().setColor(HtmlColor.AQUA);
assertEquals(HtmlColor.ORANGE, ColorAlgorithm.getFirstMissingColor(g.getAllEdges()));
}
}

0 comments on commit f86acf6

Please sign in to comment.