Graphs Flashcards

bfs,dfs,parethis structure,classification of edges,dags,topicla sortung,scc (21 cards)

1
Q

what is the pesdocode for BFS

A

INITIALIZATION
for each vertex u ≠ s:
u.color = WHITE, u.d = ∞, u.π = NIL
s.color = GRAY, s.d = 0, s.π = NIL
Q = empty queue
ENQUEUE(Q, s)

TRAVERSAL
while Q not empty:
u = DEQUEUE(Q)
for each neighbor v of u:
if v.color == WHITE:
v.color = GRAY
v.d = u.d + 1
v.π = u
ENQUEUE(Q, v)
u.color = BLACK # COMPLETION

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Initialize BFS

A

“All vertices WHITE/∞/NIL, start GRAY/0/NIL, enqueue start.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Process queue

A

Dequeue u, check neighbors, update if WHITE, enqueue, mark u BLACK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

“White Distant Nil

A

for each vertex u ≠ s:
u.color = WHITE, u.d = ∞, u.π = NIL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

“Gray Zero Nil Queue” (Start node setup)

A

s.color = GRAY, s.d = 0, s.π = NIL
Q = empty queue
ENQUEUE(Q, s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dequeue, Check, Gray, Update, Enqueue, Black” (Core loop).

A

while Q not empty:
u = DEQUEUE(Q)
for each neighbor v of u:
if v.color == WHITE:
v.color = GRAY
v.d = u.d + 1
v.π = u
ENQUEUE(Q, v)
u.color = BLACK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is running time for BFS ?

A

O(V + E), where V is the number of vertices (nodes) and E is the number of edges in the graph.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

see picture in book pg 617.

A

end result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

when would use BFS?

A

Shortest Path in Unweighted Graphs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the pesdocode for DFS

A

DFS(G):
1. for each vertex u ∈ G:V // Loop through each vertex in the graph
2. u:color = WHITE // Set the color to white (unvisited)
3. u:π = NIL // No predecessor initially
4. time = 0 // Initialize time counter
5. for each vertex u ∈ G:V // Loop through each vertex again
6. if u:color == WHITE // If the vertex is unvisited
7. DFS-VISIT(G, u) // Start a DFS visit from that vertex

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Show the d and values that result from running breadth-first search on the directed graph of Figure 22.2(a), using vertex 3 as the source

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When color precedes time i visit

A

W:for each vertex u ∈ G:V // Loop through each vertex in the graph

C: u:color = WHITE // Set the color to white (unvisited)

P: u:π = NIL // No predecessor initially

T: time =0;

I: for each vertex u ∈ G:V
if u:color == WHITE

V: DFS-Visit(G, u)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the pesdocude for DFS-Visit(G,u)

A

DFS-VISIT(G, u):
1. time = time + 1 // White vertex u has just been discovered
2. u:d = time // Record discovery time of u
3. u:color = GRAY // Mark vertex u as discovered (in progress)
4. for each v ∈ G:Adj[u] // Explore each edge (u, v)
5. if v:color == WHITE // If v is unvisited
6. v:π = u // Set u as the predecessor of v
7. DFS-VISIT(G, v) // Recursively visit v
8. u:color = BLACK // Blacken u; it is finished
9. time = time + 1 // Increment time
10. u:f = time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Time Don’t Get Grumpy. Visit When Processing Finishes.”

A

Time Don’t Get Grumpy. Visit When Processing Finishes.”

“Time” → Increment the time as soon as you discover u.

“Don’t” → Record the discovery time of u.

“Get” → Set u’s color to GRAY (discovered, in progress).

“Explore” → Explore each neighbor of u.

“Visit” → Check if the neighbor is WHITE, and visit it if it is.

“When” → Set u as the predecessor of v.

“Rain” → Recursively visit the neighbor v.

“Finishes” → Once finished with neighbors, set u’s color to BLACK.

“Finishes” → Increment the time as you finish processing u.

“Finishes” → Record the finish time for u.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the running time for DFS ?

A

O(V + E)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Show the parenthesis structure of the depth-first search of Figure 22.4. pg 632

17
Q

what is pesdocode for Toploical sort

A

TOPOLOGICAL-SORT.G/
1 call DFS.G/ to compute finishing times :f for each vertex
2 as each vertex is finished, insert it onto the front of a linked list
3 return the linked list of vertices

18
Q

what is running time for toplocoalg sort ?

19
Q

lemma: A directed graph G is acyclic if and only if a depth-first search of G yields no back
edges.

20
Q

what is pesdocde code for sccs

A

STRONGLY-CONNECTED-COMPONENTS.G/
1 call DFS.G/ to compute finishing times u:f for each vertex u
2 compute GT
3 call DFS.GT/, but in the main loop of DFS, consider the vertices
in order of decreasing u:f (as computed in line 1)
4 output the vertices of each tree in the depth-first forest formed in line 3 as a
separate strongly connected component