-
Notifications
You must be signed in to change notification settings - Fork 20k
Add WelshPowell
(Graph Colouring)
#5034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e230856
Welsh Powell Algorithm + Test
straf10 080904b
Welsh Powell Algorithm+Test second commit to fit pull request
straf10 47b77fd
Welsh Powell Algorithm+Test second commit to fit pull request
straf10 60e2d10
3rd commit
straf10 5868035
4th commit
straf10 30c4cdc
5th attempt
straf10 6200b82
6th attempt
straf10 037fc3a
7th attempt
straf10 da6d43a
Update src/main/java/com/thealgorithms/datastructures/graphs/Welsh_Po…
straf10 7328b1b
8th attempt
straf10 86f4ecd
Merge remote-tracking branch 'origin/master'
straf10 c1ae53a
9th commit(removed main and changed the test class)
straf10 b3e036f
Merge branch 'master' into master
straf10 a7f3fcd
Update src/main/java/com/thealgorithms/datastructures/graphs/WelshPow…
straf10 f59fb89
implemented Hashset, changed name to src,dst to nodeA,B added some me…
straf10 4039a2f
implemented Hashset, changed name to src,dst to nodeA,B added some me…
straf10 68cefa1
clang format
straf10 ba19ccb
added 4 new methods to improve the code, test class still unchanged b…
straf10 207bb9f
implemented the new changes. Test class probably needs more work
straf10 01db1c3
changed the addEdge method to disallow self loops(nodeA==nodeB), the …
straf10 a5e876f
added all the tests except one
straf10 c71082a
preColored test
straf10 016b561
reverted the findColoring
straf10 a55e7eb
added more complex test, blank color method
straf10 90288e7
refactor: use `isBlank`
vil02 863c4dc
Merge branch 'master' into master
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/main/java/com/thealgorithms/datastructures/graphs/Welsh_Powell.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.thealgorithms.datastructures.graphs; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Scanner; | ||
|
||
/* | ||
* The Welsh-Powell algorithm is a graph coloring algorithm | ||
* used for coloring a graph with the minimum number of colors. | ||
* https://en.wikipedia.org/wiki/Graph_coloring | ||
*/ | ||
public class Welsh_Powell { | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
// Input for the number of vertices in the graph | ||
System.out.print("Enter the number of vertices: "); | ||
int numVertices = scanner.nextInt(); | ||
scanner.nextLine(); | ||
// Initialize the graph with the given number of vertices | ||
WPGraph graph = new WPGraph(numVertices); | ||
|
||
// Input for the number of edges | ||
System.out.print("Enter the number of edges: "); | ||
int numEdges = scanner.nextInt(); | ||
scanner.nextLine(); | ||
// Adding edges to the graph based on user input | ||
for (int i = 0; i < numEdges; i++) { | ||
System.out.print("Enter both vertices for edge " + (i + 1) + " (format: 'src dest'): "); | ||
String[] vertices = scanner.nextLine().split(" "); | ||
int src = Integer.parseInt(vertices[0]); | ||
int dest = Integer.parseInt(vertices[1]); | ||
|
||
graph.addEdge(src, dest); | ||
} | ||
|
||
// Apply Welsh Powell coloring algorithm | ||
int colors[] = graph.welshPowellColoring(); | ||
|
||
// Output the color of each vertex | ||
for (int i = 0; i < numVertices; i++) { | ||
System.out.println("Vertex " + (i + 1) + " is colored with color " + colors[i]); | ||
} | ||
|
||
scanner.close(); | ||
} | ||
straf10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static class WPGraph { | ||
|
||
private int numVer; // Number of vertices in the graph | ||
public LinkedList<Integer>[] adjLists; // Adjacency lists for the graph | ||
|
||
// Constructor to initialize the graph with a certain number of vertices | ||
public WPGraph(int vertices) { | ||
numVer = vertices; | ||
adjLists = new LinkedList[vertices]; | ||
|
||
for (int i = 0; i < vertices; i++) { | ||
adjLists[i] = new LinkedList<>(); | ||
} | ||
} | ||
|
||
// Method to add an edge to the graph (undirected) | ||
public void addEdge(int src, int dest) { | ||
// Check to prevent duplicate edges | ||
if (!adjLists[src].contains(dest)) { | ||
adjLists[src].add(dest); | ||
adjLists[dest].add(src); // For undirected graphs, add the reverse edge as well | ||
} | ||
} | ||
|
||
// Method implementing Welsh Powell coloring algorithm | ||
public int[] welshPowellColoring() { | ||
int[] colors = new int[numVer]; // Array to store color of each vertex | ||
for (int i = 0; i < numVer; i++) { | ||
colors[i] = -1; // Initialize all vertices as uncolored | ||
} | ||
|
||
// Creating a list of vertices sorted by descending degree | ||
LinkedList<Integer> sortedVertices = new LinkedList<>(); | ||
for (int i = 0; i < numVer; i++) { | ||
sortedVertices.add(i); | ||
} | ||
sortedVertices.sort((v1, v2) -> adjLists[v2].size() - adjLists[v1].size()); | ||
|
||
// Coloring vertices | ||
for (int vertex : sortedVertices) { | ||
if (colors[vertex] == -1) { | ||
boolean[] usedColors = new boolean[numVer]; // Track used colors | ||
for (int neighbor : adjLists[vertex]) { | ||
if (colors[neighbor] != -1) { | ||
usedColors[colors[neighbor]] = true; // Mark neighbor's color as used | ||
} | ||
} | ||
|
||
// Assign the first unused color | ||
for (int color = 0; color < numVer; color++) { | ||
if (!usedColors[color]) { | ||
colors[vertex] = color; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return colors; // Return the array of colors for each vertex | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/thealgorithms/datastructures/graphs/WelshPowellAlgorithmTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.thealgorithms.datastructures.graphs; | ||
|
||
import com.thealgorithms.datastructures.graphs.Welsh_Powell.WPGraph; | ||
public class WelshPowellAlgorithmTest { | ||
|
||
public static void main(String[] args) { | ||
// Creating the graph for the test case | ||
int numVertices = 6; // Number of vertices | ||
WPGraph graph = new WPGraph(numVertices); | ||
|
||
// Adding the edges | ||
graph.addEdge(0, 1); | ||
graph.addEdge(0, 2); | ||
graph.addEdge(1, 3); | ||
graph.addEdge(2, 3); | ||
graph.addEdge(2, 4); | ||
graph.addEdge(3, 4); | ||
graph.addEdge(3, 5); | ||
graph.addEdge(4, 5); | ||
|
||
// Executing the method | ||
int[] colors = graph.welshPowellColoring(); | ||
|
||
// Checking the results | ||
boolean testPassed = true; | ||
for (int i = 0; i < numVertices; i++) { | ||
for (int neighbor : graph.adjLists[i]) { | ||
if (colors[i] == colors[neighbor]) { | ||
testPassed = false; | ||
break; | ||
} | ||
} | ||
if (!testPassed) { | ||
break; | ||
} | ||
} | ||
|
||
if (testPassed) { | ||
System.out.println("Test Passed."); | ||
} else { | ||
System.out.println("Test Failed."); | ||
} | ||
} | ||
} | ||
straf10 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.