From 03462aa99d7e4630cbd3815160f81c16ca7c9d1b Mon Sep 17 00:00:00 2001 From: "Norman Enrico C. Eulin" Date: Tue, 18 Feb 2025 11:20:43 +0800 Subject: [PATCH 1/2] Greedy Algorithm and Dynamic Programming Approach --- Dynamic Programming/Graph.java | 28 ++++++++++ Dynamic Programming/GraphColoringDP.java | 66 ++++++++++++++++++++++ Dynamic Programming/Main.java | 54 ++++++++++++++++++ Dynamic Programming/Timer.java | 16 ++++++ Greedy Algorithm Approach/Edge.java | 13 +++++ Greedy Algorithm Approach/Graph.java | 71 ++++++++++++++++++++++++ Greedy Algorithm Approach/Main.java | 37 ++++++++++++ 7 files changed, 285 insertions(+) create mode 100644 Dynamic Programming/Graph.java create mode 100644 Dynamic Programming/GraphColoringDP.java create mode 100644 Dynamic Programming/Main.java create mode 100644 Dynamic Programming/Timer.java create mode 100644 Greedy Algorithm Approach/Edge.java create mode 100644 Greedy Algorithm Approach/Graph.java create mode 100644 Greedy Algorithm Approach/Main.java diff --git a/Dynamic Programming/Graph.java b/Dynamic Programming/Graph.java new file mode 100644 index 0000000..4c1625b --- /dev/null +++ b/Dynamic Programming/Graph.java @@ -0,0 +1,28 @@ +import java.util.*; + +public class Graph { + int vertices; + List> adjList; + + public Graph(int vertices) { + this.vertices = vertices; + adjList = new ArrayList<>(); + for (int i = 0; i < vertices; i++) { + adjList.add(new ArrayList<>()); + } + } + + public void addEdge(int u, int v) { + adjList.get(u).add(v); + adjList.get(v).add(u); // Undirected graph + } + + public void printEdges() { + for (int u = 0; u < vertices; u++) { + for (int v : adjList.get(u)) { + if (u < v) // Avoid printing duplicate edges + System.out.println("{" + u + ", " + v + "}"); + } + } + } +} diff --git a/Dynamic Programming/GraphColoringDP.java b/Dynamic Programming/GraphColoringDP.java new file mode 100644 index 0000000..02dfb05 --- /dev/null +++ b/Dynamic Programming/GraphColoringDP.java @@ -0,0 +1,66 @@ +import java.util.Arrays; + +public class GraphColoringDP { + private Graph graph; + private int numColors; + private int[] color; + private int[][] dp; + + public GraphColoringDP(Graph graph, int numColors) { + this.graph = graph; + this.numColors = numColors; + this.color = new int[graph.vertices]; + Arrays.fill(color, -1); // Initialize color array + + // DP table to store results (initialized with -1) + this.dp = new int[graph.vertices][numColors + 1]; + for (int[] row : dp) { + Arrays.fill(row, -1); + } + } + + public boolean colorGraph() { + return solve(0, -1); + } + + private boolean solve(int v, int lastColor) { + if (v == graph.vertices) { + return true; // All vertices colored successfully + } + + if (lastColor != -1 && dp[v][lastColor] != -1) { + return dp[v][lastColor] == 1; // Return memoized result + } + + for (int c = 1; c <= numColors; c++) { + if (isSafe(v, c)) { + color[v] = c; // Assign color + if (solve(v + 1, c)) { + return true; // If successful, return true + } + color[v] = -1; // Backtrack + } + } + + if (lastColor != -1) { + dp[v][lastColor] = 0; // Memoize failure case + } + + return false; + } + + private boolean isSafe(int v, int c) { + for (int neighbor : graph.adjList.get(v)) { + if (color[neighbor] == c) { + return false; // Conflict + } + } + return true; + } + + public void printColorAssignment() { + for (int v = 0; v < graph.vertices; v++) { + System.out.println("Vertex " + v + " -> Color " + color[v]); + } + } +} diff --git a/Dynamic Programming/Main.java b/Dynamic Programming/Main.java new file mode 100644 index 0000000..c53236e --- /dev/null +++ b/Dynamic Programming/Main.java @@ -0,0 +1,54 @@ +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // User input for number of vertices and edges + System.out.print("Enter number of vertices: "); + int vertices = scanner.nextInt(); + 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 colors = scanner.nextInt(); + + // Display edges + System.out.println("\nList of edges:"); + graph.printEdges(); + + // Start timing the execution + Timer.start(); + + // Solve graph coloring + GraphColoringDP solver = new GraphColoringDP(graph, colors); + boolean solutionExists = solver.colorGraph(); + + // Stop timing + Timer.stop(); + + // Display results + if (solutionExists) { + System.out.println("\nSolution Exists: Color Assignment"); + solver.printColorAssignment(); + } else { + System.out.println("\nNo solution exists with given number of colors."); + } + + // Display execution time + System.out.printf("Execution Time: %.5f seconds\n", Timer.getTime()); + + scanner.close(); + } +} diff --git a/Dynamic Programming/Timer.java b/Dynamic Programming/Timer.java new file mode 100644 index 0000000..1e44586 --- /dev/null +++ b/Dynamic Programming/Timer.java @@ -0,0 +1,16 @@ +public class Timer { + private static long startTime; + private static long endTime; + + public static void start() { + startTime = System.nanoTime(); + } + + public static void stop() { + endTime = System.nanoTime(); + } + + public static double getTime() { + return (endTime - startTime) / 1_000_000_000.0; // Convert nanoseconds to seconds + } +} 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(); + } +} From 8ac0f4d7dad4e9e069c160fd9c9543d58518a1f5 Mon Sep 17 00:00:00 2001 From: "Norman Enrico C. Eulin" Date: Wed, 19 Feb 2025 20:41:47 +0800 Subject: [PATCH 2/2] Delete Dynamic Programming directory --- Dynamic Programming/Graph.java | 28 ---------- Dynamic Programming/GraphColoringDP.java | 66 ------------------------ Dynamic Programming/Main.java | 54 ------------------- Dynamic Programming/Timer.java | 16 ------ 4 files changed, 164 deletions(-) delete mode 100644 Dynamic Programming/Graph.java delete mode 100644 Dynamic Programming/GraphColoringDP.java delete mode 100644 Dynamic Programming/Main.java delete mode 100644 Dynamic Programming/Timer.java diff --git a/Dynamic Programming/Graph.java b/Dynamic Programming/Graph.java deleted file mode 100644 index 4c1625b..0000000 --- a/Dynamic Programming/Graph.java +++ /dev/null @@ -1,28 +0,0 @@ -import java.util.*; - -public class Graph { - int vertices; - List> adjList; - - public Graph(int vertices) { - this.vertices = vertices; - adjList = new ArrayList<>(); - for (int i = 0; i < vertices; i++) { - adjList.add(new ArrayList<>()); - } - } - - public void addEdge(int u, int v) { - adjList.get(u).add(v); - adjList.get(v).add(u); // Undirected graph - } - - public void printEdges() { - for (int u = 0; u < vertices; u++) { - for (int v : adjList.get(u)) { - if (u < v) // Avoid printing duplicate edges - System.out.println("{" + u + ", " + v + "}"); - } - } - } -} diff --git a/Dynamic Programming/GraphColoringDP.java b/Dynamic Programming/GraphColoringDP.java deleted file mode 100644 index 02dfb05..0000000 --- a/Dynamic Programming/GraphColoringDP.java +++ /dev/null @@ -1,66 +0,0 @@ -import java.util.Arrays; - -public class GraphColoringDP { - private Graph graph; - private int numColors; - private int[] color; - private int[][] dp; - - public GraphColoringDP(Graph graph, int numColors) { - this.graph = graph; - this.numColors = numColors; - this.color = new int[graph.vertices]; - Arrays.fill(color, -1); // Initialize color array - - // DP table to store results (initialized with -1) - this.dp = new int[graph.vertices][numColors + 1]; - for (int[] row : dp) { - Arrays.fill(row, -1); - } - } - - public boolean colorGraph() { - return solve(0, -1); - } - - private boolean solve(int v, int lastColor) { - if (v == graph.vertices) { - return true; // All vertices colored successfully - } - - if (lastColor != -1 && dp[v][lastColor] != -1) { - return dp[v][lastColor] == 1; // Return memoized result - } - - for (int c = 1; c <= numColors; c++) { - if (isSafe(v, c)) { - color[v] = c; // Assign color - if (solve(v + 1, c)) { - return true; // If successful, return true - } - color[v] = -1; // Backtrack - } - } - - if (lastColor != -1) { - dp[v][lastColor] = 0; // Memoize failure case - } - - return false; - } - - private boolean isSafe(int v, int c) { - for (int neighbor : graph.adjList.get(v)) { - if (color[neighbor] == c) { - return false; // Conflict - } - } - return true; - } - - public void printColorAssignment() { - for (int v = 0; v < graph.vertices; v++) { - System.out.println("Vertex " + v + " -> Color " + color[v]); - } - } -} diff --git a/Dynamic Programming/Main.java b/Dynamic Programming/Main.java deleted file mode 100644 index c53236e..0000000 --- a/Dynamic Programming/Main.java +++ /dev/null @@ -1,54 +0,0 @@ -import java.util.*; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - // User input for number of vertices and edges - System.out.print("Enter number of vertices: "); - int vertices = scanner.nextInt(); - 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 colors = scanner.nextInt(); - - // Display edges - System.out.println("\nList of edges:"); - graph.printEdges(); - - // Start timing the execution - Timer.start(); - - // Solve graph coloring - GraphColoringDP solver = new GraphColoringDP(graph, colors); - boolean solutionExists = solver.colorGraph(); - - // Stop timing - Timer.stop(); - - // Display results - if (solutionExists) { - System.out.println("\nSolution Exists: Color Assignment"); - solver.printColorAssignment(); - } else { - System.out.println("\nNo solution exists with given number of colors."); - } - - // Display execution time - System.out.printf("Execution Time: %.5f seconds\n", Timer.getTime()); - - scanner.close(); - } -} diff --git a/Dynamic Programming/Timer.java b/Dynamic Programming/Timer.java deleted file mode 100644 index 1e44586..0000000 --- a/Dynamic Programming/Timer.java +++ /dev/null @@ -1,16 +0,0 @@ -public class Timer { - private static long startTime; - private static long endTime; - - public static void start() { - startTime = System.nanoTime(); - } - - public static void stop() { - endTime = System.nanoTime(); - } - - public static double getTime() { - return (endTime - startTime) / 1_000_000_000.0; // Convert nanoseconds to seconds - } -}