Graph Algorithms: Understanding Tree, Back, Forward, and Cross Edges in Depth-First Search, Slides of Computer Science

A comprehensive review of graph algorithms, focusing on depth-first search (dfs) and the distinction among tree, back, forward, and cross edges. It includes explanations, diagrams, and code examples to help students understand the concepts.

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv ๐Ÿ‡ฎ๐Ÿ‡ณ

4.3

(12)

194 documents

1 / 64

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Graph Algorithms
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40

Partial preview of the text

Download Graph Algorithms: Understanding Tree, Back, Forward, and Cross Edges in Depth-First Search and more Slides Computer Science in PDF only on Docsity!

Algorithms

Graph Algorithms

Administrative โ— Test postponed to Friday โ— Homework: โ–  Turned in last night by midnight: full credit โ–  Turned in tonight by midnight: 1 day late, 10% off โ–  Turned in tomorrow night: 2 days late, 30% off โ–  Extra credit lateness measured separately

Review: Representing Graphs โ— Assume V = {1, 2, โ€ฆ, n } โ— An adjacency matrix represents the graph as a n x n matrix A: โ–  A[ i , j ] = 1 if edge ( i , j ) โˆˆ E (or weight of edge) = 0 if edge ( i , j ) โˆ‰ E โ–  Storage requirements: O(V 2 ) โ—‹ A dense representation โ–  But, can be very efficient for small graphs โ—‹ Especially if store just one bit/edge โ—‹ Undirected graph: only need one diagonal of matrix

Review: Graph Searching โ— Given: a graph G = (V, E), directed or undirected โ— Goal: methodically explore every vertex and every edge โ— Ultimately: build a tree on the graph โ–  Pick a vertex as the root โ–  Choose certain edges to produce a tree โ–  Note: might also build a forest if graph is not connected

Review: Breadth-First Search โ— Again will associate vertex โ€œcolorsโ€ to guide the algorithm โ–  White vertices have not been discovered โ—‹ All vertices start out white โ–  Grey vertices are discovered but not fully explored โ—‹ They may be adjacent to white vertices โ–  Black vertices are discovered and fully explored โ—‹ They are adjacent only to black and gray vertices โ— Explore vertices by scanning adjacency list of grey vertices

Review: Breadth-First Search BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v โˆˆ u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What does v->p represent? What does v->d represent?

Breadth-First Search: Example โˆž โˆž

r s t u v w x y Q:^ s

Breadth-First Search: Example 1 โˆž

r s t u v w x y Q: w r

Breadth-First Search: Example 1 2

r s t u v w x y Q: t^ x^ v

Breadth-First Search: Example 1 2

r s t u v w x y Q: x^ v^ u

Breadth-First Search: Example 1 2

r s t u v w x y Q: u^ y

Breadth-First Search: Example 1 2

r s t u v w x y Q: y

BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v โˆˆ u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the running time? Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vertโ€™s adjacency list Total running time: O(V+E)

BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v โˆˆ u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the storage cost in addition to storing the graph? Total space used: O(max(degree(v))) = O(E)