Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Greedy Algorithm Approach/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Edge {
int src, dest;

public Edge(int src, int dest) {
this.src = src;
this.dest = dest;
}

@Override
public String toString() {
return "{" + src + ", " + dest + "}";
}
}
71 changes: 71 additions & 0 deletions Greedy Algorithm Approach/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.*;

public class Graph {
private final int vertices;
private final List<List<Integer>> adjList;
private final List<Edge> edges;

public Graph(int vertices) {
this.vertices = vertices;
this.adjList = new ArrayList<>();
this.edges = new ArrayList<>();
for (int i = 0; i < vertices; i++) {
adjList.add(new ArrayList<>());
}
}

// Add edge to graph
public void addEdge(int u, int v) {
adjList.get(u).add(v);
adjList.get(v).add(u);
edges.add(new Edge(u, v));
}

// Print the edges
public void printEdges() {
System.out.println("\nList of edges:");
for (Edge edge : edges) {
System.out.print(edge + " ");
}
System.out.println("\n");
}

// Greedy Graph Coloring Algorithm
public void greedyColoring(int m) {
long startTime = System.nanoTime();

int[] colors = new int[vertices];
Arrays.fill(colors, -1); // Initialize all vertices as uncolored

// Assign colors to vertices one by one
for (int v = 0; v < vertices; v++) {
boolean[] available = new boolean[m];
Arrays.fill(available, true); // All colors are available initially

// Check colors of adjacent vertices
for (int neighbor : adjList.get(v)) {
if (colors[neighbor] != -1) {
available[colors[neighbor]] = false;
}
}

// Assign the smallest available color
for (int c = 0; c < m; c++) {
if (available[c]) {
colors[v] = c;
break;
}
}
}

long endTime = System.nanoTime();
double executionTime = (endTime - startTime) / 1e6; // Convert to milliseconds

// Print the coloring solution
System.out.println("Solution Exists: Color Assignment");
for (int v = 0; v < vertices; v++) {
System.out.println("Vertex " + v + " -> Color " + (colors[v] + 1));
}
System.out.printf("Execution Time: %.5f ms\n", executionTime);
}
}
37 changes: 37 additions & 0 deletions Greedy Algorithm Approach/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input number of vertices
System.out.print("Enter number of vertices: ");
int vertices = scanner.nextInt();

// Input number of edges
System.out.print("Enter number of edges: ");
int edges = scanner.nextInt();

Graph graph = new Graph(vertices);

// Input edges
System.out.println("Enter edges (u, v): ");
for (int i = 0; i < edges; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
graph.addEdge(u, v);
}

// Input number of colors
System.out.print("Enter number of colors: ");
int m = scanner.nextInt();

// Print the edges
graph.printEdges();

// Run greedy graph coloring
graph.greedyColoring(m);

scanner.close();
}
}