init repo
This commit is contained in:
commit
599a631c65
17 changed files with 943 additions and 0 deletions
39
app/build.gradle.kts
Normal file
39
app/build.gradle.kts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Java application project to get you started.
|
||||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.13/userguide/building_java_projects.html in the Gradle documentation.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the application plugin to add support for building a CLI application in Java.
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation(libs.junit.jupiter)
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation(libs.guava)
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = "fun.youthlic.Graph"
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
}
|
||||
324
app/src/main/java/fun/youthlic/Graph.java
Normal file
324
app/src/main/java/fun/youthlic/Graph.java
Normal file
|
|
@ -0,0 +1,324 @@
|
|||
package fun.youthlic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Random;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Graph {
|
||||
|
||||
public record EdgeRecord(String node1, String node2, int weight) {
|
||||
}
|
||||
|
||||
private static class Node {
|
||||
|
||||
String id;
|
||||
Map<Node, Integer> edges;
|
||||
|
||||
Node(final String id) {
|
||||
this.id = id;
|
||||
this.edges = new HashMap<>();
|
||||
}
|
||||
|
||||
void addEdge(final Node target) {
|
||||
edges.put(target, edges.getOrDefault(target, 0) + 1);
|
||||
}
|
||||
|
||||
List<String> getDOTEdges(final Function<EdgeRecord, String> callback) {
|
||||
final var dotEdges = new ArrayList<String>();
|
||||
for (final var entry : edges.entrySet()) {
|
||||
final Node target = entry.getKey();
|
||||
final int weight = entry.getValue();
|
||||
dotEdges.add(
|
||||
callback.apply(new EdgeRecord(id, target.id, weight)) + ";\n");
|
||||
}
|
||||
return dotEdges;
|
||||
}
|
||||
|
||||
List<String> getDOTEdges() {
|
||||
final var dotEdges = new ArrayList<String>();
|
||||
for (final var entry : edges.entrySet()) {
|
||||
final Node target = entry.getKey();
|
||||
final int weight = entry.getValue();
|
||||
dotEdges.add(
|
||||
String.format("%s -> %s [label=\"%d\"];\n", id, target.id, weight));
|
||||
}
|
||||
return dotEdges;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(final String[] args) {
|
||||
final var g = new Graph(
|
||||
"To explore strange new worlds, To seek out new life and new civilizations");
|
||||
final var words = g.findBridgeWords("team", "so");
|
||||
System.out.println(words);
|
||||
final var path = g.findShortestPath("the", "shared");
|
||||
System.out.println(path);
|
||||
System.out.println(
|
||||
g.toDOT(r -> String.format(
|
||||
"\"%s\" -> \"%s\" [label=\"%d\"%s]",
|
||||
r.node1 + (path.contains(r.node1) ? "~" : ""),
|
||||
r.node2 + (path.contains(r.node2) ? "~" : ""),
|
||||
r.weight,
|
||||
(path.contains(r.node1) &&
|
||||
path.contains(r.node2) &&
|
||||
IntStream.range(0, path.size() - 1).anyMatch(
|
||||
i -> path.get(i).equals(r.node1) && path.get(i + 1).equals(r.node2)))
|
||||
? ",color=\"red\""
|
||||
: "")));
|
||||
System.out.println(g.computePageRank("new", 0.85));
|
||||
System.out.println(g.randomWalk());
|
||||
}
|
||||
|
||||
private final Map<String, Node> nodes;
|
||||
|
||||
private Map<Node, Double> pageRanks;
|
||||
|
||||
public Graph(final String input, final double d) {
|
||||
nodes = new HashMap<>();
|
||||
parse(input);
|
||||
initRageRanks(d);
|
||||
}
|
||||
|
||||
public Graph(final String input) {
|
||||
nodes = new HashMap<>();
|
||||
parse(input);
|
||||
initRageRanks(0.85);
|
||||
}
|
||||
|
||||
public Optional<List<String>> findBridgeWords(final String word1, final String word2) {
|
||||
final Node node1 = nodes.get(word1.toLowerCase(Locale.ENGLISH));
|
||||
final Node node2 = nodes.get(word2.toLowerCase(Locale.ENGLISH));
|
||||
|
||||
if (node1 == null || node2 == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final var bridgeWords = new ArrayList<String>();
|
||||
for (final var node : node1.edges.keySet()) {
|
||||
if (node.edges.containsKey(node2)) {
|
||||
bridgeWords.add(node.id);
|
||||
}
|
||||
}
|
||||
return Optional.of(bridgeWords);
|
||||
}
|
||||
|
||||
public List<String> findShortestPath(final String word1, final String word2) {
|
||||
final var startWord = word1.toLowerCase(Locale.ENGLISH);
|
||||
final var endWord = word2.toLowerCase(Locale.ENGLISH);
|
||||
final var path = new ArrayList<String>();
|
||||
|
||||
if (!nodes.containsKey(startWord) || !nodes.containsKey(endWord)) {
|
||||
return path;
|
||||
}
|
||||
|
||||
final Node startNode = nodes.get(startWord);
|
||||
final Node endNode = nodes.get(endWord);
|
||||
|
||||
if (startNode == endNode) {
|
||||
path.add(startNode.id);
|
||||
return path;
|
||||
}
|
||||
|
||||
final var distances = new HashMap<Node, Integer>();
|
||||
final var previousNodes = new HashMap<Node, Node>();
|
||||
final var queue = new PriorityQueue<Node>(
|
||||
Comparator.comparingInt(node -> distances.getOrDefault(node, Integer.MAX_VALUE)));
|
||||
|
||||
for (final var node : nodes.values()) {
|
||||
distances.put(node, Integer.MAX_VALUE);
|
||||
}
|
||||
distances.put(startNode, 0);
|
||||
queue.add(startNode);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
final Node current = queue.poll();
|
||||
for (final var edge : current.edges.entrySet()) {
|
||||
final Node neighbor = edge.getKey();
|
||||
final int weight = edge.getValue();
|
||||
|
||||
final int newDist = distances.get(current) + weight;
|
||||
if (newDist < distances.get(neighbor)) {
|
||||
distances.put(neighbor, newDist);
|
||||
previousNodes.put(neighbor, current);
|
||||
queue.add(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (distances.get(endNode) == Integer.MAX_VALUE) {
|
||||
return path;
|
||||
}
|
||||
|
||||
final var reversePath = new LinkedList<String>();
|
||||
Node current = endNode;
|
||||
while (current != null) {
|
||||
reversePath.addFirst(current.id);
|
||||
current = previousNodes.get(current);
|
||||
}
|
||||
|
||||
return reversePath;
|
||||
}
|
||||
|
||||
public double computePageRank(final String nodeId, final double d) {
|
||||
final Node node = nodes.get(nodeId);
|
||||
if (node == null) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return pageRanks.getOrDefault(node, 0.0);
|
||||
}
|
||||
|
||||
public List<String> randomWalk() {
|
||||
final var path = new ArrayList<String>();
|
||||
if (nodes.isEmpty()) {
|
||||
return path;
|
||||
}
|
||||
|
||||
final var random = new Random();
|
||||
final var nodeIds = new ArrayList<>(nodes.keySet());
|
||||
final String startNodeId = nodeIds.get(random.nextInt(nodeIds.size()));
|
||||
path.add(startNodeId);
|
||||
|
||||
final var usedEdges = new HashMap<String, Integer>();
|
||||
var currentNode = nodes.get(startNodeId);
|
||||
while (true) {
|
||||
final var availableEdge = new ArrayList<Map.Entry<Node, Integer>>();
|
||||
int totalWeight = 0;
|
||||
for (final var entry : currentNode.edges.entrySet()) {
|
||||
final String edgeKey = currentNode.id + " -> " + entry.getKey().id;
|
||||
if (usedEdges.getOrDefault(edgeKey, 0) < 2) {
|
||||
availableEdge.add(entry);
|
||||
totalWeight += entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
if (availableEdge.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
final double randomValue = random.nextDouble() * totalWeight;
|
||||
double cumulative = 0.0;
|
||||
Node nextNode = null;
|
||||
|
||||
for (final var entry : availableEdge) {
|
||||
cumulative += entry.getValue();
|
||||
if (randomValue < cumulative) {
|
||||
nextNode = entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert nextNode != null;
|
||||
final String edgeKey = currentNode.id + " -> " + nextNode.id;
|
||||
final int edgeTimes = usedEdges.getOrDefault(edgeKey, 0) + 1;
|
||||
usedEdges.put(edgeKey, edgeTimes);
|
||||
path.add(nextNode.id);
|
||||
currentNode = nextNode;
|
||||
|
||||
if (edgeTimes >= 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public String toDOT(final Function<EdgeRecord, String> callback) {
|
||||
final var sb = new StringBuilder();
|
||||
sb.append("digraph G {\n");
|
||||
for (final Node node : nodes.values()) {
|
||||
for (final String edge : node.getDOTEdges(callback)) {
|
||||
sb.append(" ").append(edge);
|
||||
}
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String toDOT() {
|
||||
final var sb = new StringBuilder();
|
||||
sb.append("digraph G {\n");
|
||||
for (final Node node : nodes.values()) {
|
||||
for (final String edge : node.getDOTEdges()) {
|
||||
sb.append(" ").append(edge).append("\n");
|
||||
}
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void parse(final String input) {
|
||||
final String[] tokens = input.toLowerCase(Locale.ENGLISH).split("[ \n\r]+");
|
||||
final var words = new ArrayList<String>();
|
||||
|
||||
for (final var token : tokens) {
|
||||
final String word = token.replaceAll("[^a-zA-Z]", "");
|
||||
if (!word.isEmpty()) {
|
||||
words.add(word);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < words.size() - 1; i++) {
|
||||
final String currentWord = words.get(i);
|
||||
final String nextWord = words.get(i + 1);
|
||||
|
||||
final Node currentNode = getOrCreateNode(currentWord);
|
||||
final Node nextNode = getOrCreateNode(nextWord);
|
||||
|
||||
currentNode.addEdge(nextNode);
|
||||
}
|
||||
}
|
||||
|
||||
private Node getOrCreateNode(final String word) {
|
||||
return nodes.computeIfAbsent(word, Node::new);
|
||||
}
|
||||
|
||||
private void initRageRanks(final double d) {
|
||||
final int totalNodes = nodes.size();
|
||||
if (totalNodes == 0) {
|
||||
pageRanks = new HashMap<>();
|
||||
return;
|
||||
}
|
||||
var myPageRanks = new HashMap<Node, Double>();
|
||||
final double initRank = 1.0 / totalNodes;
|
||||
for (final var n : nodes.values()) {
|
||||
myPageRanks.put(n, initRank);
|
||||
}
|
||||
|
||||
final int iterations = 100;
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
final var newPageRanks = new HashMap<Node, Double>();
|
||||
nodes.values().forEach(n -> newPageRanks.put(n, 0.0));
|
||||
for (final var curr : nodes.values()) {
|
||||
final int totalEdges = curr.edges.size();
|
||||
|
||||
for (final var entry : curr.edges.entrySet()) {
|
||||
final Node neighbor = entry.getKey();
|
||||
final double contribution = myPageRanks.get(curr) / totalEdges;
|
||||
newPageRanks.put(neighbor, newPageRanks.get(neighbor) + contribution);
|
||||
}
|
||||
}
|
||||
|
||||
final double damplingTerm = (1.0 - d) / totalNodes;
|
||||
nodes.values().forEach(n -> newPageRanks.put(n, damplingTerm + d * newPageRanks.get(n)));
|
||||
myPageRanks = newPageRanks;
|
||||
}
|
||||
|
||||
pageRanks = myPageRanks;
|
||||
for (final var node : nodes.values()) {
|
||||
if (node.edges.isEmpty()) {
|
||||
nodes.values().stream().filter(n -> !n.equals(node))
|
||||
.forEach(n -> pageRanks.put(n, pageRanks.get(n) + pageRanks.get(node) / (totalNodes - 1)));
|
||||
pageRanks.put(node, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
0
app/src/test/java/fun/youthlic/GraphTest.java
Normal file
0
app/src/test/java/fun/youthlic/GraphTest.java
Normal file
Loading…
Add table
Add a link
Reference in a new issue