Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b21a48

Browse files
Undirected Graph
1 parent c8fff6f commit 7b21a48

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pl.dmichalski.algorithms.data_structures._10_undirected_graph
2+
3+
data class Node(private val vertex: String, private val connections: MutableList<String>) {
4+
5+
fun getVertex(): String {
6+
return vertex
7+
}
8+
9+
fun getConnections(): MutableList<String> {
10+
return connections
11+
}
12+
13+
override fun toString(): String {
14+
return "Node(vertex='$vertex', connections=$connections)"
15+
}
16+
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package pl.dmichalski.algorithms.data_structures._10_undirected_graph
2+
3+
object Runner {
4+
5+
private const val VERTEX_A = "A"
6+
private const val VERTEX_B = "B"
7+
private const val VERTEX_C = "C"
8+
9+
@JvmStatic
10+
fun main(args: Array<String>) {
11+
println("------------------ Initial undirected graph ------------------ ")
12+
var undirectedGraph = getUndirectedGraph()
13+
printUndirectedGraph(undirectedGraph)
14+
15+
println("------------------ After adding some edges ------------------ ")
16+
undirectedGraph = getUndirectedGraph()
17+
undirectedGraph.addEdge(VERTEX_B, VERTEX_C)
18+
undirectedGraph.addEdge(VERTEX_A, VERTEX_C)
19+
printUndirectedGraph(undirectedGraph)
20+
21+
println("------------------ After removing an edge ------------------ ")
22+
undirectedGraph = getUndirectedGraph()
23+
undirectedGraph.removeEdge(VERTEX_A, VERTEX_B)
24+
printUndirectedGraph(undirectedGraph)
25+
26+
println("------------------ After removing a vertex ------------------ ")
27+
undirectedGraph = getUndirectedGraph()
28+
undirectedGraph.removeVertex(VERTEX_A)
29+
printUndirectedGraph(undirectedGraph)
30+
}
31+
32+
private fun getUndirectedGraph(): UndirectedGraph {
33+
val undirectedGraph = UndirectedGraph()
34+
35+
undirectedGraph.addVertex(VERTEX_A)
36+
undirectedGraph.addVertex(VERTEX_B)
37+
undirectedGraph.addVertex(VERTEX_C)
38+
39+
undirectedGraph.addEdge(VERTEX_A, VERTEX_B)
40+
41+
return undirectedGraph
42+
}
43+
44+
private fun printUndirectedGraph(undirectedGraph: UndirectedGraph) {
45+
println(undirectedGraph)
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package pl.dmichalski.algorithms.data_structures._10_undirected_graph
2+
3+
class UndirectedGraph {
4+
5+
private val adjacencyList: MutableList<Node> = ArrayList()
6+
7+
fun addVertex(vertex: String) {
8+
val node = findNode(vertex)
9+
10+
if (node == null) {
11+
val newNode = Node(vertex, mutableListOf())
12+
this.adjacencyList.add(newNode)
13+
}
14+
}
15+
16+
fun addEdge(vertex1: String, vertex2: String) {
17+
val node1 = findNode(vertex1)
18+
val node2 = findNode(vertex2)
19+
20+
node1!!.getConnections().add(vertex2)
21+
node2!!.getConnections().add(vertex1)
22+
}
23+
24+
fun removeVertex(vertex: String) {
25+
val node = findNode(vertex)
26+
adjacencyList.forEach { it.getConnections().remove(vertex) }
27+
adjacencyList.remove(node)
28+
}
29+
30+
fun removeEdge(vertex1: String, vertex2: String) {
31+
val node1 = findNode(vertex1)
32+
val node2 = findNode(vertex2)
33+
34+
node1!!.getConnections().remove(vertex2)
35+
node2!!.getConnections().remove(vertex1)
36+
}
37+
38+
override fun toString(): String {
39+
return "UndirectedGraph(adjacencyList='$adjacencyList')\n"
40+
}
41+
42+
private fun findNode(vertex: String): Node? {
43+
return adjacencyList.stream()
44+
.filter { node -> node.getVertex() == vertex }
45+
.findFirst()
46+
.orElse(null)
47+
}
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /