From 304abd92f5d2fc62e2186230e6c8577d544b3f6d Mon Sep 17 00:00:00 2001 From: Geoffrey Challen Date: Thu, 28 Oct 2021 14:30:19 -0500 Subject: [PATCH] Working on graphs. --- .idea/runConfigurations/Local_Publish.xml | 21 ++++++++++++ .../{cs125 => cs1}/graphs/UndirectedGraph.kt | 32 ++++++++++++++----- src/test/java/com/example/Example.java | 2 +- src/test/kotlin/TestUndirectedGraph.kt | 16 ++++++---- 4 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 .idea/runConfigurations/Local_Publish.xml rename src/main/java/{cs125 => cs1}/graphs/UndirectedGraph.kt (89%) diff --git a/.idea/runConfigurations/Local_Publish.xml b/.idea/runConfigurations/Local_Publish.xml new file mode 100644 index 0000000..f107c1d --- /dev/null +++ b/.idea/runConfigurations/Local_Publish.xml @@ -0,0 +1,21 @@ + + + + + + + true + true + false + + + \ No newline at end of file diff --git a/src/main/java/cs125/graphs/UndirectedGraph.kt b/src/main/java/cs1/graphs/UndirectedGraph.kt similarity index 89% rename from src/main/java/cs125/graphs/UndirectedGraph.kt rename to src/main/java/cs1/graphs/UndirectedGraph.kt index 07a83be..a72ff7f 100644 --- a/src/main/java/cs125/graphs/UndirectedGraph.kt +++ b/src/main/java/cs1/graphs/UndirectedGraph.kt @@ -1,26 +1,42 @@ -package cs125.graphs +package cs1.graphs + +import java.util.Objects class Node(val value: T, private val index: Int, val neighbors: MutableSet> = mutableSetOf()) { override fun toString() = "Node $index ($value)" + override fun equals(other: Any?): Boolean { + if (other !is Node<*>) { + return false + } + return value == other.value && toMap() == other.toMap() + } + + override fun hashCode() = Objects.hash(value, toMap()) } class InputNode(var value: T) -private fun find(node: Node, visited: MutableSet>) { - visited += node - for (neighbor in node.neighbors) { +private fun Node.find(visited: MutableSet>) { + visited += this + for (neighbor in neighbors) { if (neighbor !in visited) { - find(neighbor, visited) + neighbor.find(visited) } } } -private fun find(node: Node): Set> { +private fun Node.find(): Set> { val nodes = mutableSetOf>() - find(node, nodes) + println(nodes.size) + find(nodes) return nodes } +fun Node.toMap(): Map, Set>> { + find() + return mapOf, Set>>() +} + fun Map, Set>>.toGraph(): Node { check(isNotEmpty()) { "Graph is empty" } val mapping = keys.mapIndexed { index, key -> key to Node(key.value, index) }.toMap() @@ -35,7 +51,7 @@ fun Map, Set>>.toGraph(): Node { mapping.values.forEach { check(it !in it.neighbors) { "Graph contains a self-edge" } - check(find(it) == mapping.values.toSet()) { "Graph is not connected" } + check(it.find() == mapping.values.toSet()) { "Graph is not connected" } for (node in it.neighbors) { check(it in node.neighbors) { "Graph is not undirected" } } diff --git a/src/test/java/com/example/Example.java b/src/test/java/com/example/Example.java index 646a7fe..39ebb15 100644 --- a/src/test/java/com/example/Example.java +++ b/src/test/java/com/example/Example.java @@ -1,6 +1,6 @@ package com.example; -import cs125.graphs.Node; +import cs1.graphs.Node; import java.util.HashSet; import java.util.Set; diff --git a/src/test/kotlin/TestUndirectedGraph.kt b/src/test/kotlin/TestUndirectedGraph.kt index 76f94c4..71008d1 100644 --- a/src/test/kotlin/TestUndirectedGraph.kt +++ b/src/test/kotlin/TestUndirectedGraph.kt @@ -1,16 +1,18 @@ import com.example.Example -import cs125.graphs.circleGraph -import cs125.graphs.fullyConnectedGraph -import cs125.graphs.singleNodeGraph -import cs125.graphs.twoNodeGraph +import cs1.graphs.circleGraph +import cs1.graphs.fullyConnectedGraph +import cs1.graphs.singleNodeGraph +import cs1.graphs.toMap +import cs1.graphs.twoNodeGraph import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe class TestUndirectedGraph : StringSpec({ - "it should create a single-node graph" { + "f: it should create a single-node graph" { singleNodeGraph(8).also { - Example.size(it) shouldBe 1 - Example.sum(it) shouldBe 8 + println(it.toMap()) + //Example.size(it) shouldBe 1 + //Example.sum(it) shouldBe 8 } } "it should create a two-node graph" {