Permalink
Browse files

Add files via upload

  • Loading branch information...
shah314 committed May 21, 2018
1 parent 2604c39 commit f53df593aca3ceef6ff10c748f89921fc959c3fa
Showing with 12,528 additions and 0 deletions.
  1. +177 −0 Backtracking.java
  2. +168 −0 Clique.java
  3. +53 −0 Constants.java
  4. +78 −0 Graph.java
  5. +152 −0 GraphColoring.java
  6. +54 −0 GraphReader.java
  7. +278 −0 IteratedGreedy.java
  8. +145 −0 LocalSearch.java
  9. +200 −0 MaxClique.java
  10. +193 −0 Node.java
  11. +30 −0 SortedListNode.java
  12. +10,397 −0 data.clq
  13. +603 −0 data1.clq
View
@@ -0,0 +1,177 @@
import java.util.*;
/**
* Backtracking solver for the graph coloring problem.
* Spends a specific amount of time (Constants.TIME) on trying to color the graph
* using k colors after which it increments k by 1.
* If k is known, it can be populated (Constants.KNOWN_K).
* The time it spends on each k before incrementing it is very important for the performance of the
* algorithm. For large graphs, the time should be increased to at least 10000 ms.
* For small dense graphs, 2000 ms should work.
*
* @author Shalin Shah
* Email: shah.shalin@gmail.com
*/
public class Backtracking {
/** Creates a new instance of GraphColoring */
public Backtracking() {
}
public static void main(String [] args) throws Exception
{
System.out.println("Reading Graph...");
/* Read the graph from Constants.FILE */
Graph graph = GraphReader.readGraph();
/* Compute Clique */
LinkedHashSet clique = MaxClique.computeClique(graph);
/* Color the vertices of the clique with different colors */
int k = clique.size();
if(Constants.KNOWN_K > 0)
k = Constants.KNOWN_K;
Iterator it = clique.iterator();
int col = 0;
while(it.hasNext())
{
int vertex = ((Integer)it.next()).intValue();
Node node = graph.nodes[vertex];
node.colorNode(col);
col++;
}
/* Try to color the nodes with increasing values of k */
while(true)
{
List uncoloredNodes = new ArrayList();
Node [] nodes = graph.nodes;
for(int i=0; i<nodes.length; i++)
{
Node node = nodes[i];
if(node.color == Constants.UNCOLORED)
{
node.computePossibleColors(graph, k);
uncoloredNodes.add(node);
}
}
PossibleColorsComparator comparator = new PossibleColorsComparator();
Collections.sort(uncoloredNodes, comparator);
Node previous = null;
for(int i=0; i<uncoloredNodes.size();i++)
{
Node node = (Node)uncoloredNodes.get(i);
if(previous == null)
{
previous = node;
node.previous = null;
}
else
{
previous.next = node;
node.previous = previous;
previous = node;
}
}
Node head = (Node)uncoloredNodes.get(0);
/* Try to color the uncolored nodes using k colors */
Node current = (Node)uncoloredNodes.get(0);
Node last = (Node)uncoloredNodes.get(uncoloredNodes.size()-1);
boolean solved = false;
System.out.println("Trying to color graph with " + k + " colors...");
long startTime = System.currentTimeMillis();
while(true)
{
long endTime = System.currentTimeMillis();
if((endTime-startTime) > Constants.TIME)
{
solved = false;
break;
}
if(current == last)
{
int nextColor = current.nextColor();
while(nextColor != Constants.UNCOLORED && !current.isValidColor(graph, nextColor))
{
nextColor = current.nextColor();
}
if(nextColor == Constants.UNCOLORED)
{
current.resetColorCount();
current = current.previous;
}
else
{
/* Solved */
current.colorNode(nextColor);
System.out.println("\n" + k + " coloring found! Exiting...");
solved = true;
break;
}
}
else
{
int nextColor = current.nextColor();
while(nextColor != Constants.UNCOLORED && !current.isValidColor(graph, nextColor))
{
nextColor = current.nextColor();
}
if(nextColor == Constants.UNCOLORED)
{
current.resetColorCount();
current = current.previous;
if(current == null)
{
solved = false;
break;
}
}
else
{
current.colorNode(nextColor);
current = current.next();
}
}
}
if(solved)
{
break;
}
else
{
k++;
}
}
}
public static class PossibleColorsComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
Node node1 = (Node)o1;
Node node2 = (Node)o2;
if(node1.possibleColors.size() < node2.possibleColors.size())
{
return 1;
}
else if(node1.possibleColors.size() > node2.possibleColors.size())
{
return -1;
}
else
{
return 0;
}
}
}
}
View
@@ -0,0 +1,168 @@
import java.util.*;
/**
* Abstraction of a Clique - A solution to the clique problem.
*
* @author Shalin Shah
* Email: shah.shalin@gmail.com
*/
public class Clique
{
/* Possible additions */
public LinkedHashSet pa;
/* The vertices that make up the clique */
public LinkedHashSet clique;
/* A TreeMap of possible additions sorted according to their degrees */
public TreeMap sortedPA;
/* The graph on which the algorithm runs */
public Graph graph;
/* A random access list of the vertices that make up the clique */
public List cliqueList;
/** Creates a new instance of Clique */
public Clique(int firstVertex, Graph graph)
{
int [][] aMatrix = graph.aMatrix;
pa = new LinkedHashSet();
clique = new LinkedHashSet();
sortedPA = new TreeMap();
cliqueList = new ArrayList();
clique.add(new Integer(firstVertex));
cliqueList.add(new Integer(firstVertex));
this.graph = graph;
for(int i=0; i<Constants.NUMBER_NODES; i++)
{
if(i == firstVertex)
{
continue;
}
if(aMatrix[i][firstVertex] == 1)
{
pa.add(new Integer(i));
sortedPA.put(graph.nodes[i], graph.nodes[i]);
}
}
}
/*
* Add a vertex to the clique and update the possible additions list.
*/
public void addVertex(int vertex, int [][] aMatrix)
{
clique.add(new Integer(vertex));
cliqueList.add(new Integer(vertex));
removeFromSortedPA(graph.nodes[vertex]);
pa.remove(new Integer(vertex));
for(Iterator it = pa.iterator(); it.hasNext();)
{
int pavertex = ((Integer)it.next()).intValue();
if(aMatrix[pavertex][vertex] == 0)
{
it.remove();
removeFromSortedPA(graph.nodes[pavertex]);
}
else if(aMatrix[pavertex][vertex] == 1)
{
// do nothing
}
else
{
System.out.println("Invalid Matrix in addVertex of Clique");
System.exit(1);
}
}
}
/*
* Remove a vertex from the clique and update the possible additions list.
*/
public void removeVertex(int vertex, int [][]aMatrix)
{
if(!clique.contains(new Integer(vertex)))
return;
clique.remove(new Integer(vertex));
cliqueList.remove(new Integer(vertex));
for(int i=0; i<Constants.NUMBER_NODES; i++)
{
if(clique.contains(new Integer(i)))
{
continue;
}
else
{
Iterator it = clique.iterator();
boolean flag = true;
while(it.hasNext())
{
int ver = ((Integer)it.next()).intValue();
if(aMatrix[i][ver] == 0)
{
flag = false;
break;
}
}
if(flag)
{
pa.add(new Integer(i));
sortedPA.put(graph.nodes[i], graph.nodes[i]);
}
}
}
}
/* Linear time remove method for TreeMap */
private void removeFromSortedPA(Node node)
{
Set set = sortedPA.keySet();
Iterator it = set.iterator();
while(it.hasNext())
{
if(it.next().equals(node))
{
it.remove();
break;
}
}
}
/*
* Compute a list of nodes based on degrees in the induced subgraph of
* possible additions (PA)
*/
public List computeSortedList()
{
List sortedList = new ArrayList();
Iterator it = pa.iterator();
while(it.hasNext())
{
int node1 = ((Integer)it.next()).intValue();
int reach = 0;
Iterator itt = pa.iterator();
while(itt.hasNext())
{
int node2 = ((Integer)itt.next()).intValue();
if(graph.aMatrix[node1][node2] == 1)
{
reach++;
}
}
SortedListNode n = new SortedListNode();
n.reach = reach;
n.node = node1;
sortedList.add(n);
}
Collections.sort(sortedList);
return sortedList;
}
}
Oops, something went wrong.

0 comments on commit f53df59

Please sign in to comment.