Repository navigation
Expand file tree
/
Copy pathGraph.kt
More file actions
140 lines (112 loc) · 3.08 KB
/
Copy pathGraph.kt
File metadata and controls
140 lines (112 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.*
/**
* Created by ynassar on 20/06/2017.
*/
fun main(args: Array<String>) {
val europe = Graph<String>("EU")
europe.add("Spain","Portugal","England","France","Wales")
println("Israel is in EU? ${"Israel" in europe}")
europe.connect("Spain","Portugal")
europe.connect("Portugal","France")
europe.connect("France","England")
europe.dfs("France") { println(it) }
}
data class Person(val name: String, var age: Int){
private var isMarried: Boolean = false
set(value) {
println("Setting isMarried field")
field = value
}
get() {
println("Getting isMarried field")
return field
}
operator fun plus(other: Person): Person {
return Person(name + other.name, age + other.age)
}
}
class Node<T>(val key: T) {
private val adjacents: LinkedList<Node<T>> = LinkedList()
fun bfs(f: (Node<T>) -> Unit){
val q = LinkedList<Node<T>>()
val visited = HashMap<Node<T>,Boolean>()
q.offer(this)
while(q.isNotEmpty()) {
val n = q.pop()
f(n)
visited[n] = true
n.adjacents.forEach {
if(visited[it] != true){
q.offer(it)
}
}
}
}
fun dfs(f: (Node<T>) -> Unit) {
val stack = Stack<Node<T>>()
val visited = HashMap<Node<T>,Boolean>()
stack.push(this)
while(stack.isNotEmpty()) {
val n = stack.pop()
f(n)
visited[n] = true
n.adjacents.forEach {
if(visited[it] != true) {
stack.push(it)
}
}
}
}
fun addAdjacent(node: Node<T>) {
adjacents.add(node)
}
override fun toString(): String = "Node: $key"
}
class Graph<T>(val id: String){
private val nodes: LinkedList<Node<T>>
init{
println("Graph $id init")
nodes = LinkedList()
}
operator fun contains(key: T): Boolean{
return nodes.any { it.key == key }
}
operator fun plus(other: Graph<T>): Graph<T> {
var res = Graph<T>(id)
res.addNodes(nodes)
res.addNodes(other.nodes)
return res
}
private operator fun get(key: T): Node<T>? {
return nodes.firstOrNull { it.key === key }
}
private fun addNodes(nds: List<Node<T>>) {
nodes.addAll(nds)
}
private fun addNode(n: Node<T>) {
nodes.add(n)
}
fun add(vararg keys: T) {
nodes.addAll(keys.map { Node(it) })
}
fun equals(other: Graph<T>): Boolean {
return nodes.containsAll(other.nodes)
}
fun bfs(key: T, f: (Node<T>) -> Unit) {
val node = this[key]
node?.bfs(f)
}
fun dfs(key: T, f: (Node<T>) -> Unit) {
val node = this[key]
node?.dfs(f)
}
fun connect(key1: T, key2: T){
if(key1 !in this || key2 !in this) {
return
}
val node1 = this[key1]
val node2 = this[key2]
node1?.addAdjacent(node2!!)
node2?.addAdjacent(node1!!)
}
}