diff --git a/Greedy Algorithm Approach/Edge.java b/Greedy Algorithm Approach/Edge.java new file mode 100644 index 0000000..5522a65 --- /dev/null +++ b/Greedy Algorithm Approach/Edge.java @@ -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 + "}"; + } +} diff --git a/Greedy Algorithm Approach/Graph.java b/Greedy Algorithm Approach/Graph.java new file mode 100644 index 0000000..e13ea1b --- /dev/null +++ b/Greedy Algorithm Approach/Graph.java @@ -0,0 +1,71 @@ +import java.util.*; + +public class Graph { + private final int vertices; + private final List> adjList; + private final List 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); + } +} diff --git a/Greedy Algorithm Approach/Main.java b/Greedy Algorithm Approach/Main.java new file mode 100644 index 0000000..7358306 --- /dev/null +++ b/Greedy Algorithm Approach/Main.java @@ -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(); + } +}