-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create post order,IN order, Pre order, DFS,BFS examples in Kotlin #21
base: master
Are you sure you want to change the base?
Conversation
@vibhu172000 please, put some description to your PR. |
DFS-
Graph
9 7
0 1
1 2
1 3
2 3
3 4
6 7
6 8
Result
Component 1 0 1 2 3 4
Component 2 5
Component 3 6 7 8
There are 3 connected components
The important things worth mentioning here is the use of `forEach`with `it`
variable and ranges from Kotlin helped me quite a lot. Use of `Pair` and
variable like `tv.first` is also helpful.
enum class Color {
BLACK, WHITE
}
var vert: MutableList<Color> = mutableListOf()
var adj: MutableList<MutableList<Pair<Int, Int>>> = mutableListOf()
vert is a list of `Color` enum which is used track the visit status of each
node. WHITE denotes if it’s unvisited while BLACK means it’s visited. adj
maintains are graph in the form of adjacency list which `list of list of
Pairs`. Pair contains two numbers of which first denotes the node and
second denotes the weight associated with that edge which is in this case
is `1`.
the core DFS is very small
fun dfs(u: Int) {
vert[u] = Color.BLACK
print(" $u")
(0..adj[u].size-1).forEach {
val tv = adj[u][it]
if(vert[tv.first] == Color.WHITE) dfs(tv.first)
}
}
BFS
Graph
9 7
0 1
1 2
1 3
2 3
3 4
6 7
6 8
Result
u: 0 and [Node(color=BLACK, distance=0, parent=-1), Node(color=BLACK,
distance=1, parent=1), Node(color=BLACK, distance=2, parent=2),
Node(color=BLACK, distance=2, parent=3), Node(color=BLACK, distance=3,
parent=4)]
u: 6 and [Node(color=BLACK, distance=0, parent=-1), Node(color=BLACK,
distance=1, parent=7), Node(color=BLACK, distance=1, parent=8)]
u: 5 and [Node(color=BLACK, distance=0, parent=-1)]
use the following variables and enum class.
enum class Color {
BLACK, WHITE
}
var adj: MutableList<MutableList<Pair<Int, Int>>> = mutableListOf()
We make use of a `data class Node` to keep track of color for visiting,
distance and parent of the node while doing BFS.
…On Wed, Oct 28, 2020, 05:47 Greg Kesler ***@***.*** wrote:
@vibhu172000 <https://github.com/vibhu172000> please, put some
description to your PR.
What does it solve? From the provided code I can tel that you put examples
how to recursively traverse a tree. Sure, you can do that. How is that
related to this project?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/APYLMOUQM2IFZVIR5YZPTUDSM5PIDANCNFSM4SUAYAFQ>
.
|
@vibhu172000 thanks again, I saw this in another PR. Still, the question is: how this is related to traverser? Have you tried to implement your dfs/bfs using traverser component from this project? |
Actually I have decided an example in previous mail of bed, dfs in kotlin.
…On Wed, Oct 28, 2020, 09:24 Greg Kesler ***@***.*** wrote:
@vibhu172000 <https://github.com/vibhu172000> thanks again, I saw this in
another PR. Still, the question is: how this is related to traverser? Have
you tried to implement your def/bfs using traverser component from this
project?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/APYLMOXMSI5KZMSPXD24AVDSM6IXNANCNFSM4SUAYAFQ>
.
|
1. Issue Link: #17
2. Brief explanation of a change:
3. Will it break existing clients and code in production? YES