#BFS and DFS
1 messages ยท Page 1 of 1 (latest)
<@&987246746478460948> please have a look, thanks.
ur supposed to draw a picture of the spanning trees that BFS and DFS would yield if performed on these graphs
whats unclear to u? do u know what a spanning tree is? adjacency matrix? graph? bfs? dfs?
I know what adjacency amtrix is
I can draw tree with this matrix
but
what am I supposed to do after that
ur not supposed to draw the graph
although if u can do that as a first step, it would be helpful
what is spanning tree then I dont know
ur task is to draw the spanning tree that bfs/dfs would form while being executed on that graph
do u know bfs and dfs?
breadth first search and depth first search
yeah but do u know how these algos work?
kinda
they visit the nodes of the graph in a particular order and that order, if u draw it down, forms a tree
that's a spanning tree, as it spans the graph
wait let me draw graoh for it
yes. draw the graph first, that will help u
and ull also notice what they mention in part 2
the graph isnt connected and ur spanning trees will show that depending on which node u started the bfs/dfs at
since it wont span the entire graph, only a subgraph
is that the graph? why the two colors?
why not draw on the grapth itself ?
on what?
let's first draw the graph itself
not the spanning trees or bfs/dfs stuff
step by step ๐
u got two graphs, A and B
id say we focus on A for now
or did I misunderstood ?
its an adjacency matrix. it's like OP attempted
its a graph
u need the arrowheads, or is it bidirectional?
not a tree
its not supposed to be a tree
here is with matrix
but this time take care of the directions
looks like its mirrored diagonally, wouldnt that mean bidirectional?
so graph is correct?
looks like it. but are u sure u understood that part? especially with the directions
like, u cant necessarily say that if 1 is connected to 2 that 2 is also connected to 1
if column 1 has a 1 entry in row 2,that doesn't mean that column 2 has a 1 entry in row 1 as well
edges have a direction and can't necessarily be traveled in both directions
ok
think about one way streets
not sure what u mean with A and B. but my point is that u just drew ur graph with no arrowheads on ur edges
assuming each edge would exist in both directions
which isnt necessarily given
well that depends if the graph allows it or not
like if we go from 1 to 2 we can't go back to 1
that depends on the graph, it defines if its allowed or not
at first this is kinda unrelated to adjancency matrices
u have to understand how to read the adjacency matrix. it defines twice as many edges as u drew, technically
yes
because when I drew it once it jsut reoeated
like when I connected 5 to 8
I didn't connect 8 to 5 again
yeah exactly. but technically these are two different edges
with direction
one from 5 to 8 and a second from 8 to 5
and they are directional
ur adjacency matrix has it so that all edges are doubled, but thats not the case for all adjacency matrices necessarily
just saying
ok
okay, so far so good
it not only tells you which nodes are connected, but also how they are connected
now, pick a node. perhaps node 3. and perform BFS starting from it
step by step
take note of the order in which it visits/explores notes
ok let me try
draw it as u do bfs in ur head
for example ull start with node 3. next it will put nodes 4 and 8 into its queue
in the next step of bfs it might poll node 4
node 4 has no new neighbors, so that branch ends
bfs polls 8 next from the queue
and then adds nodes 5, 6 and 7 to the queue
I started from node 3 and finished on node 2
that edge from 5 to 1 shouldnt be purple
yes. take note of the order
its nodes 4 and 8 first
if u do bfs
then nodes 5, 6 and 7
then nodes 1 and 2
yes. now get rid of the black edges and look only at the colored ones
and dont forget the arrows on the edges
cause this time it's definitely directional
add arrows please
ok
now reorganize it so that it visually all goes from top to bottom, i. e. move nodes 5, 6, 7, 1, 2 downwards
yup. now the reorganize step
nah i mean, change ur picture
ull see that what u have here is a tree
it becomes obvious after u draw it top to bottom
yeah. so what u have here is a so called spanning tree (rooted at node 3). it spans over the graph
yeah
and if u do dfs instead of bfs it will look different as well
this is how I perform bfs?
so. they just want u to draw some of these spanning trees rooted at various nodes u can pick at random
perhaps pick 2 nodes, do dfs, then bfs. do it on graph A and then on graph B, ull end up with 8 pictures of various spanning trees
and that's the first task
how do I perform dfs? I knew what was bfs but I have no idea about dfs
I know it goes very deep
it differs in the order, thats all
bfs uses a FIFO queue for the nodes to visit
dfs a LIFO
that's all
stacks?
so u just perform the same algorithm but with a LIFO in ur head
yes, a stack is an example for a lifo queue
Graph traversal can be accomplished easily using BFS or DFS. The algorithms only differ in the order in which nodes are visited: https://i.imgur.com/n9WrkQG.png
The code to accomplish them is identical and only differs in the behavior of the Queue they are based on. BFS uses a FIFO-queue and DFS a LIFO-queue.
Queue<Node> nodesToProcess = ... // depending on BFS or DFS
nodesToProcess.add(rootNode); // add all starting nodes
Set<Node> visitedNodes = new HashSet<>();
while (!nodesToProcess.isEmpty()) {
// Settle node
Node currentNode = nodesToProcess.poll();
if (!visitedNodes.add(currentNode)) {
continue; // Already visited before
}
// Do something with the node
System.out.println(currentNode); // Replace by whatever you need
// Relax all outgoing edges
for (Node neighbor : currentNode.getNeighbors()) {
nodesToProcess.add(neighbor);
}
}
To get BFS, use a FIFO-queue:
Queue<Node> nodesToProcess = new ArrayDeque<>();
And for DFS a LIFO-queue:
Queue<Node> nodesToProcess = Collections.asLifoQueue(new ArrayDeque<>());
Thats all, very simple to setup and use.
For directed graphs relax all outgoing edges.
For trees the visitedNodes logic can be dropped, since each node can only have maximally one parent, simplifying the algorithm to just:
Queue<Node> nodesToProcess = ... // depending on BFS or DFS
nodesToProcess.add(rootNode); // add all starting nodes
while (!nodesToProcess.isEmpty()) {
// Settle node
Node currentNode = nodesToProcess.poll();
// Do something with the node
System.out.println(currentNode); // Replace by whatever you need
// Relax all outgoing edges
for (Node child : currentNode.getChildren()) {
nodesToProcess.add(child);
}
}
I would visually look at it, though this is for a tree and not a graph
so in bfs when I finished with 2 now when I finish it with 2 again I will start drawing tree with 2?
do it step by step. start at node 3
it adds nodes 4 and 8
then it polls node 4
so that part is the same
node 4 is a leaf, so no new nodes added
next is node 8
now it adds nodes 5 6 7.
that would be bfs?
it adds all of them but they are polled in different order now
it polls node 5 now, adds nodes 1 next
bfs would now poll node 6
but dfs polls node 1
so 1 is relaxed before 6
but its a leaf, so whatever
sounds wrong to me how you explain it
the picture ur drawing isnt right
nodes 6 and 7 werent relaxed yet
only added to the queue
not polled
what? dfs uses stack not queue?
the difference between bfs and dfs is that one uses a fifo queue and the other a lifo queue
a stack is a queue
a lifo queue to be specific
well sure
makes it hard to understand though imo
whatever wont disrupt this anymore 
either case, the spanning trees formed by both are identical
the coloring is different. but the structure is the same
what am I doing next
ull end up with the same image
so what is difference then
the order in which u explored nodes. so if u color each edge in order, it will be different
is that it can go back?
not sure what u mean
like this
and what I got from the picuute other guy send it means we can go back
technically every edge needs its own color. in both dfs and bfs
if u give some the same color that's incorrect
like this or smth
no. the spanning tree formed by bfs/dfs isnt a representation of how the algo moves
yeha but when I will be doing this on my final exams I can't have miltiple color pencils
its rather the edges explored by the algo as it walks along
u dont need to color anything. that's just for u to understand stuff
if u want to understand bfs vs dfs its about order
but the spanning tree is the same
ok
example:
this is the order in which bfs walks along these edges
dfs would be
order is different
but the spanning tree is in both cases just
I thought it was much harder
chatGPT explained it way harder
for example if u start at node 8 the spanning tree is a different tree
yeah it will look totally different
so that's task 1, just drawing a bunch of these
task 2 wanta u to think about graphs that aren't connected
that is, u can end up in dead ends
a graph where some nodes can't reach all other nodes
this is what it looks like with starting node of 8
an extreme example would be two totally independent mini graphs in one graph that just sit next to each other with no direct connection
but less extreme examples would be literal dead ends where a node leads to another node with a one way street with no way out for example
I think that is the question 2 on my screenshot
there is no graph here
depending on which node u start ull notice that the spanning tree wont include all nodes of the graph
it will just include all nodes reachable from the starting node
once u understand this connection task 2 is simple
it wants u to show that using bfs/dfs and spanning trees will tell u if a graph is connected or not
is either bfs or dfs considered greedy?
thats another task though, not task 2. isnt it
no they are not
I know one dijstra
dijkstra is the adaption of bfs for weighted graphs
its bfs but with a priority queue of tentative distances
instead of a regular fifo queue
ur supposed to find a minimal spanning tree in that task
i. e. a spanning tree that has the least amount of edges
I am not that advanced to do that task
if you can help with 2nd part of my bfs and dfs
okay. so first make urself a graph that isnt fully connected
and then do some bfs with different starting nodes, drawing the spanning trees
ull notice that the spanning trees wont include all nodes of the graph then
please dont go for bidirectional/unidirectional graphs all the time
that will make this harder
make a graph with some well connected cluster plus a one way street instead
this?
what is bidirectiona; ๐ญ
make a circle and then a one way street line extending out of it
ur making edges that can be walked in both directions
edges have a single direction only
ur always leaving out the arrowheads
oh u want me to draw bipartite graph?
no
that's sth else
ur drawing lines instead of arrows
edges have a direction
indicated by an arrow
so u want me to draw arros instead of edges?
yes
better
that example isnt will suited though. don't make a fully lonely node
draw 5 nodes connected in a circle
connect them to a circle please
๐ญ
that's not a circle, its two one way streets ending in the node on the bottom left
change the direction of the edges so it forms a circle please
okay. so now we have a connected graph. now we add stuff to it that isnt fully connected
add two more nodes on the top which the circle extends to
with a one way street
think of a dead end street connected to a roundabout
ok
now, make bfs on node 2
then bfs on node 6
draw the spanning trees for both
and pay attention to the direction of the edges as u do that. bfs cant explore edges backwards
edges can only be travelled in one direction
so. what u witnessed now is that doing bfs will only give u nodes that are reachable from the start (obviously)
u forgot the arrows ๐
so. if u do these bfs/spanning tree things and ull notice that ull get a tree that doesnt include all nodes, it means that the graph isnt fully connected
i.e. some nodes cant reach all other nodes
task 2 wants u to show this.
now, i lack context of this task and ur course to tell u whether ur supposed to mathematically prove this or perhaps to just make an example (like we just did together) and draw a picture or two