# CS代考 DFS 23[1,2] – cscodehelp代写

Tutorial 9: Graph basics
• Simulate BFS and DFS for some specific graphs
• See a more advanced DFS-based algorithm

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
(for simplicity, assume adjacency lists are sorted)

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3  45

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3  45

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3  45

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3  45

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3 [] 45

In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3 [] 45
Notice how the next element selected is the first that was inserted: BFS uses a queue to select the next vertex

In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
BFS 2 3 [] 45
One way to think about it: I visit node 1, then everyone that has distance 1 to node 1, then everyone that has distance 2 to node 1…

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
DFS 23[1,2]

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
DFS 23[1,2]

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
[1,2,3,5,4]

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
DFS 23[1,2]

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
Notice how the next element selected is the last that was inserted: DFS uses a stack to select the next vertex

In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
One way to think about it: think of it like exploring a videogame, you can only come back from a room after exploring everything available

In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
Now let us try to imagine what the BFS and DFS tree for some graphs look like without doing it step by step

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
8 9 10 11 12 13 14 15 16

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.
8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• In this problem you will have to describe the depth and breadth first search trees calculated for particular graphs.

• Exercise on bridges

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph Describe an O(E.V) algorithm to find all the bridges in a graph

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph Describe an O(E.V) algorithm to find all the bridges in a graph Try all the edges

• Exercise on bridges
Bridge is an edge whose removal disconnects the graph Describe an O(EV) algorithm to find all the bridges in a graph Try all the edges

Nice observation:
An edge is a bridge if and only if it is part of a cycle in G

Nice observation:
An edge is a bridge if and only if it is part of a cycle in G
Is there a path from 1 to 2 without (1,2)?

• Exercise on bridges
Describe an algorithm to find all the bridges in a graph
Equivalent to
Describe an algorithm to find all edges that are not part of a cycle in a graph

Describe an algorithm to find all edges that are not part of a cycle in a graph
Think of the DFS tree: we know there are no cross edges, only tree edges and back edges

Describe an algorithm to find all edges that are not part of a cycle in a graph
Think of the DFS tree: we know there are no cross edges, only tree edges and back edges

Describe an algorithm to find all edges that are not part of a cycle in a graph
Think of the DFS tree: we know there are no cross edges, only tree edges and back edges

Describe an algorithm to find all edges that are not part of a cycle in a graph
Think of the DFS tree: we know there are no cross edges, only tree edges and back edges

Describe an algorithm to find all edges that are not part of a cycle in a graph
Think of the DFS tree: we know there are no cross edges, only tree edges and back edges
A back edge “creates” a cycle, so no edge from v to u can be a bridges

Describe an algorithm to find all edges that are not part of a cycle in a graph
Think of the DFS tree: we know there are no cross edges, only tree edges and back edges
A back edge “creates” a cycle, so no edge from v to u can be a bridges

How do we make that into an algorithm?

How do we make that into an algorithm?
An edge (v,u) is a bridge if and only if none of it’s descendants (in the DFS tree) has a back-edge to v or any ancestors of v

How do we make that into an algorithm?
An edge (v,u) is a bridge if and only if none of it’s descendants (in the DFS tree) has a back-edge to v or any ancestors of v
So we calculate: low[u] -> what is the highest vertex we can achieve by
going down the DFS tree and up one back-edge

How do we make that into an algorithm?
An edge (v,u) is a bridge if and only if none of it’s descendants (in the DFS tree) has a back-edge to v or any ancestors of v
So we calculate: low[u] -> what is the highest vertex we can achieve by
going down the DFS tree and up one back-edge If low[u] = u, then (v,u) is a bridge
(there is no other path from u to v)

How do we make that into an algorithm?
An edge (v,u) is a bridge if and only if none of it’s descendants (in the DFS tree) has a back-edge to v or any ancestors of v
So we calculate: low[u] -> what is the highest vertex we can achieve by
going down the DFS tree and up one back-edge If low[u] = u, then (v,u) is a bridge
(there is no other path from u to v)
Else, (v,u) is part of a cycle in G
(going to that highest vertex and back down to v is another path from u to v)

low[u] -> what is the highest vertex we can achieve by going down the DFS tree and up one back-edge
How do we calculate low[u]?

low[u] -> what is the highest vertex we can achieve by going down the DFS tree and up one back-edge
How do we calculate low[u]?
DP-like: it is the highest node among the back-edges from u, and low of u’s children

low[u] -> what is the highest vertex we can achieve by going down the DFS tree and up one back-edge
How do we calculate low[u]?
DP-like: it is the highest node among the back-edges from u, and low of u’s children
We only need one DFS, therefore the algorithm if O(E+V)