-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rbaul
authored and
rbaul
committed
Feb 6, 2024
1 parent
7e5cb33
commit f30b4c7
Showing
8 changed files
with
127 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
47 changes: 47 additions & 0 deletions
47
...ion/src/main/java/com/github/rbaul/microservice_visualization/service/AlgorithmUtils.java
This file contains 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,47 @@ | ||
package com.github.rbaul.microservice_visualization.service; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jgrapht.Graph; | ||
import org.jgrapht.GraphPath; | ||
import org.jgrapht.alg.cycle.PatonCycleBase; | ||
import org.jgrapht.graph.DefaultEdge; | ||
import org.jgrapht.graph.SimpleGraph; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@UtilityClass | ||
public class AlgorithmUtils { | ||
|
||
// Function to find all simple cycles in an undirected graph using Paton's algorithm | ||
public Set<List<String>> findSimpleCycles(Map<String, List<String>> connections) { | ||
Graph<String, CustomEdge> graph = new SimpleGraph<>(CustomEdge.class); | ||
|
||
// Vertex | ||
connections.keySet().forEach(graph::addVertex); | ||
|
||
// Edges | ||
connections.forEach((vertex, cons) -> { | ||
for (String con : cons) { | ||
graph.addEdge(vertex, con, new CustomEdge()); | ||
} | ||
}); | ||
|
||
return findSimpleCycles(graph); | ||
} | ||
|
||
// Function to find all simple cycles in an undirected graph using Paton's algorithm | ||
private Set<List<String>> findSimpleCycles(Graph<String, CustomEdge> graph) { | ||
PatonCycleBase<String, CustomEdge> cycleBase = new PatonCycleBase<>(graph); | ||
return cycleBase.getCycleBasis().getCyclesAsGraphPaths().stream().map(GraphPath::getVertexList).collect(Collectors.toSet()); | ||
} | ||
|
||
// Custom edge class (required by JGraphT for undirected graphs) | ||
private static class CustomEdge extends DefaultEdge { | ||
// You can add custom properties if needed | ||
} | ||
} |
This file contains 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
This file contains 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