Skip to content

Instantly share code, notes, and snippets.

@tareq-si-salem
Last active August 5, 2016 14:23
Show Gist options
  • Save tareq-si-salem/fff0ae61cdc551febd0d6ae41212c94c to your computer and use it in GitHub Desktop.
Save tareq-si-salem/fff0ae61cdc551febd0d6ae41212c94c to your computer and use it in GitHub Desktop.
Graph edges coloring heuristic greedy algorithm Console version
import java.util.*;
public class Main {
static ArrayList<Edge> edges;
// Since We don't know how many edges we have initially we used array list to change size dynamically.
static char colors[];
// Colors array is used to specify the corresponding color for each edge colors.length == edges.size()
static int colors_number = 0;
// An integer that specifies the number of colors needed to color the graph
public static void main(String... args) {
init();
// Initialize the graph and request graph description from user
coloration();/* Start coloring the edges of the graph such that minimum colors
used and no adjacent edges
have the same color using the greedy heuristic algorithm */
display();
// Display the color assigned to each edge
}
static void init() {
edges = new ArrayList<>();
// Create a new ArrayList object to hold the edges objects
Scanner in = new Scanner(System.in);
// We used Scanner to read input from the user
System.out.print("Number of nodes: ");
int size = in.nextInt();
// size defines the number of nodes the graph has
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
System.out.print("Node " + i + " is adjacent to node "
+ j + ": ");
char adjacent = in.next().toUpperCase().charAt(0);
// A word is read from the console and the first character is extracted
if (adjacent == 'Y') {
// Since the word is converted to capital letters 'y' or 'Y' means the
// the two nodes have an edge between them.
Edge edge = new Edge(i, j);
// create new edge, with i:nodeA and j:nodeB
edges.add(edge);// Add it to the edges list
}
}
}
}
static void coloration() {
char label_color = 'A';
// label_color is the label givenfor each colors.
// starting from 'A' of integer value 65 from the ASCII table.
colors = new char[edges.size()];
// initialize the colors array with 0 as initial value for each element.
for (int i = 0; i < colors.length; i++) {
if (colors[i] == 0) {
// If the current node isn't colored assign label_color and increment the Label
colors[i] = label_color;
colors_number++;
label_color = (char) ((int) label_color + 1);
// change label color to the lower character in the ASCII Table example: 'A' -> 'B'
}
for (int j = i + 1; j < colors.length; j++) {
// a loop to check the non adjacent edges to current edges with color[i]
if (colors[j] == 0)// check if the edge is not colored then the condition is true
{
boolean adj = false;
// a boolean variable initially false to check if the current edge
// is not adjacent to other edges with color [i]
for (int k = 0; k < colors.length; k++) {
if (k != j && colors[k] == colors[i]) {
if (isAdjacent(k, j)) {
// if the current edge [i], is adjacent to any of the edges holding color[i]
// adj is changed to true implying to not color the current edge with color[i]
adj = true;
break;
}
}
}
if (!adj) {
colors[j] = colors[i];
// assign current Edge [j] color to be the same as color[i].
}
}
}
}
}
static void display() {
System.out.println("We needed " + colors_number + " colors to colorthe edges");// display the number of colors needed to color the graph
for (int i = 0; i < colors.length; i++) {
// a loop to iterate over the edges inside the list
// and show the assigned color.
System.out.print("edge" + edges.get(i) + " color: ");
System.out.println(String.valueOf(colors[i]));
// the String.valueOf is used to convert single character to String
}
}
static boolean isAdjacent(int i, int j) {
// check if the edge:i is adjacent to edge: j
return (edges.get(j).nodeA == edges.get(i).nodeA
|| // whenever the two edges share a common node they are said
edges.get(j).nodeA == edges.get(i).nodeB
|| // to be adjacent.
edges.get(j).nodeB == edges.get(i).nodeA
|| edges.get(j).nodeB == edges.get(i).nodeB);
}
}
class Edge {
public int nodeA;
public int nodeB;// for simplicity no getters and setters are generated
// we access directly the fields
public Edge(int nodeA, int nodeB) {
// A constructor that takes two nodes as argument
this.nodeA = nodeA;
// to specify which nodes the edge is connecting
this.nodeB = nodeB;
}
@Override
public String toString() {
return "< " + nodeA + ", " + nodeB + " >";
// The Object.toString() method is overridden to help with formatting
// example for nodeA:2, nodeB:3 it returns "< 2, 3 >" to be used
// in the display method
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment