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 40e8aa6

Browse files
refactored Graph
1 parent 8945005 commit 40e8aa6

File tree

4 files changed

+190
-154
lines changed

4 files changed

+190
-154
lines changed

‎src/main/kotlin/structures/Graph.kt

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import kotlin.collections.LinkedHashSet
55

66
/**
77
*
8-
* data structure: undirected graph without weights
8+
* Graph is a non-linear data structure consisting of vertices and edges.
9+
*
10+
* The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph.
11+
*
12+
* More formally a Graph is composed of a set of vertices V and a set of edges E. The graph is denoted by G(E, V).
913
*
10-
* description: made up of vertices connected by edges
14+
* Undirected graph is a type of graph where the edges have no specified direction assigned to the them.
1115
*
1216
*/
1317

1418
class Graph<T> {
1519

16-
private val data = mutableMapOf<Vertex<T>, MutableList<Vertex<T>>>()
20+
private val data = linkedMapOf<Vertex<T>, MutableList<Vertex<T>>>()
1721

18-
/**
19-
* adds a new vertex with a value [value]
20-
*/
21-
funaddVertex(value:T) = data.putIfAbsent(Vertex(value), mutableListOf())
22+
// Complexity: O(1)
23+
funaddVertex(value:T) {
24+
data.putIfAbsent(Vertex(value), mutableListOf())
25+
}
2226

23-
/**
24-
* removes a vertex by value [value] from a graph
25-
*/
27+
// Complexity: O(n)
2628
fun removeVertex(value: T) {
2729
val removingVertex = Vertex(value)
2830
data.values.forEach { list ->
@@ -31,73 +33,74 @@ class Graph<T> {
3133
data.remove(removingVertex)
3234
}
3335

34-
/**
35-
* adds an edge between two vertices, that have values [value1], [value2]
36-
*/
36+
// Complexity: O(1)
3737
fun addEdge(value1: T, value2: T) {
3838
val vertex1 = Vertex(value1)
3939
val vertex2 = Vertex(value2)
4040
data[vertex1]?.add(vertex2)
4141
data[vertex2]?.add(vertex1)
4242
}
4343

44-
/**
45-
* removes an edge between two vertices, that have values [value1], [value2]
46-
*/
44+
// Complexity: O(1)
4745
fun removeEdge(value1: T, value2: T) {
4846
val vertex1 = Vertex(value1)
4947
val vertex2 = Vertex(value2)
5048
data[vertex1]?.remove(vertex2)
5149
data[vertex2]?.remove(vertex1)
5250
}
5351

54-
/**
55-
* returns the associated vertices with the given vertex value [value]
56-
*/
57-
fun connectedVertexes(value: T) = data[Vertex(value)] ?: listOf()
52+
// returns the associated vertices with the given vertex value
53+
fun connectedVertexes(value: T) = data[Vertex(value)]?.map { it.value } ?: emptyList()
5854

5955
/**
60-
* traversal of the graph in depth, returns all vertices of the graph
56+
*
57+
* Traversal of the graph in depth,
58+
*
59+
* returns all vertices of the graph
60+
*
6161
*/
62-
fun depthFirstTraversal() : List<Vertex<T>> {
63-
val visited = LinkedHashSet<Vertex<T>>()
64-
val queue = LinkedList<Vertex<T>>()
62+
fun depthFirstTraversal() : List<T> {
6563
val firstVertex = data.keys.firstOrNull() ?: return emptyList()
64+
65+
val visited = LinkedHashSet<T>()
66+
val queue = LinkedList<Vertex<T>>()
6667
queue.push(firstVertex)
6768
while (queue.isNotEmpty()) {
68-
val vertex = queue.poll()
69-
if (!visited.contains(vertex)) {
70-
visited.add(vertex)
71-
queue.addAll(data[vertex] ?: listOf())
69+
val vertex = queue.pollFirst()
70+
if (!visited.contains(vertex.value)) {
71+
visited.add(vertex.value)
72+
queue.addAll(data[vertex] ?: emptyList())
7273
}
7374
}
7475
return visited.toList()
7576
}
7677

7778
/**
78-
* traversal of the graph in breadth, returns all vertices of the graph
79+
*
80+
* Traversal of the graph in breadth,
81+
*
82+
* returns all vertices of the graph
83+
*
7984
*/
80-
fun breadthFirstTraversal() : List<Vertex<T>> {
81-
val visited = LinkedHashSet<Vertex<T>>()
82-
val queue = LinkedList<Vertex<T>>()
85+
fun breadthFirstTraversal() : List<T> {
8386
val firstVertex = data.keys.firstOrNull() ?: return emptyList()
87+
88+
val visited = LinkedHashSet<T>()
89+
val queue = LinkedList<Vertex<T>>()
8490
queue.add(firstVertex)
85-
visited.add(firstVertex)
91+
visited.add(firstVertex.value)
8692
while (queue.isNotEmpty()) {
87-
val vertex = queue.poll()
88-
data[vertex]?.forEach { v ->
89-
if (!visited.contains(v)) {
90-
visited.add(v)
91-
queue.add(v)
93+
val vertex = queue.pollFirst()
94+
data[vertex]?.forEach { connectedVertex ->
95+
if (!visited.contains(connectedVertex.value)) {
96+
visited.add(connectedVertex.value)
97+
queue.add(connectedVertex)
9298
}
9399
}
94100
}
95101
return visited.toList()
96102
}
97103

98-
}
104+
privatedata classVertex<T>(valvalue:T)
99105

100-
/**
101-
* graph vertex model
102-
*/
103-
data class Vertex<T>(val value: T)
106+
}
Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,74 @@
11
package structures
22

3+
34
/**
45
*
5-
* data structure: directed graph with weights
6+
* Graph is a non-linear data structure consisting of vertices and edges.
7+
*
8+
* The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph.
9+
*
10+
* More formally a Graph is composed of a set of vertices V and a set of edges E. The graph is denoted by G(E, V).
611
*
7-
* description: made up of vertices connected by edges that have direction and weight
12+
* Directed graph with weights is a type of graph where the edges have specified direction and weights assigned to the them.
813
*
914
*/
1015

1116
class GraphWithWeights<T> {
1217

1318
private val data = linkedMapOf<Vertex<T>, MutableList<VertexConnection<T>>>()
1419

15-
/**
16-
* adds a new vertex with a value [value]
17-
*/
18-
funaddVertex(value:T) = data.putIfAbsent(Vertex(value), mutableListOf())
20+
// Complexity: O(1)
21+
funaddVertex(value:T) {
22+
data.putIfAbsent(Vertex(value), mutableListOf())
23+
}
1924

20-
/**
21-
* removes a vertex by value [value] from a graph
22-
*/
25+
// Complexity: O(n)
2326
fun removeVertex(value: T) {
2427
val removingVertex = Vertex(value)
25-
data.values.forEach { list ->
26-
list.removeIf { it.vertex == removingVertex }
28+
data.values.forEach { connections ->
29+
connections.removeIf { it.vertex == removingVertex }
2730
}
2831
data.remove(removingVertex)
2932
}
3033

31-
/**
32-
* adds an edge between two vertices, that have values [value1], [value2]
33-
*/
34+
// Complexity: O(1)
3435
fun addEdge(value1: T, value2: T, cost: Int) {
3536
val vertex1 = Vertex(value1)
3637
val vertex2 = Vertex(value2)
3738
data[vertex1]?.add(VertexConnection(vertex2, cost))
3839
}
3940

40-
/**
41-
* removes an edge between two vertices, that have values [value1], [value2]
42-
*/
41+
// Complexity: O(n)
4342
fun removeEdge(value1: T, value2: T) {
4443
val vertex1 = Vertex(value1)
4544
val vertex2 = Vertex(value2)
4645
data[vertex1]?.removeIf { it.vertex == vertex2 }
4746
}
4847

49-
/**
50-
* returns the associated vertices and their weights with the given vertex value [value]
51-
*/
52-
fun connectedVertexesWithWeights(value: T) = data[Vertex(value)] ?: listOf()
48+
// returns the associated vertices and their weights with the given vertex value
49+
fun connectedVertexesWithWeights(value: T) = data[Vertex(value)]?.map { it.toString() } ?: emptyList()
5350

5451
/**
55-
* implementation of Dijkstra's algorithm, returns pairs of a vertex and the minimum weight needed to get to that vertex (the starting vertex is the first added)
52+
*
53+
* Dijkstra's algorithm,
54+
*
55+
* returns pairs of a vertex and the minimum weight needed to get to that vertex,
56+
*
57+
* the starting vertex is the first added
58+
*
5659
*/
57-
fun dijkstraAlgorithm(): Map<Vertex<T>, Int> {
60+
fun dijkstraAlgorithm(): Map<T, Int> {
61+
if (data.isEmpty()) return emptyMap()
62+
5863
val unvisitedVertexes = linkedMapOf<Vertex<T>, Int>()
5964
data.keys.forEach { vertex ->
6065
unvisitedVertexes[vertex] = Int.MAX_VALUE
6166
}
6267

63-
val visitedVertexes = linkedMapOf<Vertex<T>, Int>()
68+
val visitedVertexes = linkedMapOf<T, Int>()
6469
var minimumCost = 0
6570

66-
var currentVertex = unvisitedVertexes.keys.firstOrNull() ?:return visitedVertexes
71+
var currentVertex = unvisitedVertexes.keys.first()
6772
while(unvisitedVertexes.isNotEmpty()) {
6873
val neighbourVertexConnections = data[currentVertex] ?: emptyList()
6974
for (neighbourVertexConnection in neighbourVertexConnections) {
@@ -76,7 +81,7 @@ class GraphWithWeights<T> {
7681
unvisitedVertexes[neighbourVertex] = newCost
7782
}
7883
}
79-
visitedVertexes[currentVertex] = minimumCost
84+
visitedVertexes[currentVertex.value] = minimumCost
8085
unvisitedVertexes.remove(currentVertex)
8186
val nextUnvisitedEntry = unvisitedVertexes.entries
8287
.filter { it.value != Int.MAX_VALUE }
@@ -87,9 +92,11 @@ class GraphWithWeights<T> {
8792
return visitedVertexes
8893
}
8994

90-
}
95+
privatedata classVertex<T>(valvalue:T)
9196

92-
/**
93-
* helper class for defining graph weights
94-
*/
95-
data class VertexConnection<T>(val vertex: Vertex<T>, val cost: Int)
97+
// helper class for defining graph weights
98+
private data class VertexConnection<T>(val vertex: Vertex<T>, val cost: Int) {
99+
override fun toString(): String = "vertex -> ${vertex.value}, cost -> $cost"
100+
}
101+
102+
}

0 commit comments

Comments
(0)

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