Graph Algorithms
Chapter 22
CPTR 430 Algorithms Graph Algorithms
Graph Algorithms Chapter 22 1 CPTR 430 Algorithms Graph - - PowerPoint PPT Presentation
Graph Algorithms Chapter 22 1 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms for such an obscure area?
CPTR 430 Algorithms Graph Algorithms
■ Mathematical graphs seem to be relatively specialized and abstract ■ Why spend so much time and effort on algorithms for such an obscure
■ As it turns out, many practical problems can be solved by applying
CPTR 430 Algorithms Graph Algorithms
■ A graph is a mathematical structure that consists of a set of vertices
■ G
■
☎■
☎■ In the context of asymptotic notation, V means
☎■ For example, O
✆2
✝CPTR 430 Algorithms Graph Algorithms
■ Adjacency matrix ■ Adjacency list
CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
■ Adjacency lists are good for sparse graphs ❚ Sparse graph:
☎2 ❚ The list nodes could instead contain pointers to vertices ❚ For digraphs: sum of lengths of the lists =
☎❚ For undirected graphs: sum of lengths of the lists = 2
☎❚ Space required: Θ
✆■ Adjacency matrix is better for dense graphs ❚ Dense graph:
☎2 ❚ Can quickly tell if two vertices share an edge ❚ Space required: Θ
✆❚ Undirected graphs can use a triangular matrix ❚ Unweighted graphs require only one bit per edge!
CPTR 430 Algorithms Graph Algorithms
■ For each
✆■ w
✆■ Weighted graphs represent edge costs, lengths, time, etc. ■ Adjacency list: the edge weight is stored in the node for the edge in the
■ Adjacency matrix: the edge weight is stored in matrix
CPTR 430 Algorithms Graph Algorithms
■ Used in a number of classic graph algorithms: ❚ Prim’s minimal spanning tree ❚ Dijkstra’s single source shortest path ■ Finds all the vertices reachable from a source vertex, s ■ Produces a breadth-first tree whose root is s ■ Computes the length of the path to each reachable vertex
■ Breadth-first
CPTR 430 Algorithms Graph Algorithms
■ BFS marks vertices to manage its progress ■ Vertices are: ❚ Colored white initially ❚ Colored gray when first discovered ❚ Colored black when all their neighbors have been discovered ■
✆■ Gray
CPTR 430 Algorithms Graph Algorithms
■ BFS essentially builds a tree ❚ The tree’s root is the source vertex ❚ Vertex v discovered while scanning vertex u neighbors
CPTR 430 Algorithms Graph Algorithms
■ Augment vertices with additional information: ❚ color (black, gray, white) (color) ❚ predecessor (parent vertex in the BF tree), π
❚ distance (number of edges from root [source vertex] to this vertex),
■ Use a queue to manage the gray vertices
CPTR 430 Algorithms Graph Algorithms
■ First, for each vertex: ❚ set its color to white (undiscovered) ❚ set its predecessor to null (not in the BF tree) ❚ set its distance to ∞ (may not even be a path to it from the source
■ For the start vertex s: ❚ color s gray ❚ set d
❚ place s in the queue
CPTR 430 Algorithms Graph Algorithms
■ As long as the queue is not empty, extract a vertex u from the queue
❚ for each vertex v adjacent to u: ❙ color v gray ❙ set d
❙ set π
❙ Place v into the queue ❚ Color u black
CPTR 430 Algorithms Graph Algorithms
■ Use aggregate analysis ■ Every vertex must be colored and properly initialized
■ After initialization, no vertex is ever made white again
■ Recall that the time to enqueue and dequeue elements is O
✆CPTR 430 Algorithms Graph Algorithms
■ Each time a vertex is dequeued: ❚ its adjacent vertices (via its adjacency list) are each are checked to see
❚ a vertex’s adjacency list is checked once only ❚ the sum of the vertices in all adjacency lists is Θ
✆■
■ This means that BFS runs in time linear in the size of the (complete)
CPTR 430 Algorithms Graph Algorithms
■ Define δ
✆❚ The minimum number of edges in any path from s to v ❚ δ
■
✆ ✆❚ u is reachable from s
❚ The shortest path from s to v cannot be longer than the shortest path
❚ u is unreachable from s
CPTR 430 Algorithms Graph Algorithms
■ Proof:
■ Proceed by induction on the number of enqueue operations ❚ Basis case: Is it true after the first enqueue operation? ❚ Inductive step: True after k enqueue operations
CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
■ A vertex is enqueued only if it is white ■ Let vertex v be a white vertex discovered while considering vertex u’s
■ Vertex v must be enqueued ■ The inductive step is then
■ Once enqueued, v’s color is set to gray, so it can never be enqueued
CPTR 430 Algorithms Graph Algorithms
■ Let
✁■ v1 is the head of the queue ■ vr is the tail of the queue ■ d
■
✆1
✁CPTR 430 Algorithms Graph Algorithms
■ Basis case: Does the lemma hold after the first queue operation? ■ Inductive step: True after k queue operations
CPTR 430 Algorithms Graph Algorithms
■ The first queue operation is always enqueuing s, the source vertex ■ Here, r
❚ d
❚ d
1
✁CPTR 430 Algorithms Graph Algorithms
■ We must show it holds for both enqueue and dequeue operations ■ Queue head v1 dequeued
■ d
■ d
CPTR 430 Algorithms Graph Algorithms
■ When vertex v is enqueued, it becomes vr
✁1 ■ Vertex v is enqueued while scanning vertex u’s neighbors —
✆■ Vertex u was the queue head, but it was dequeued before its neighbors
■ By the I.H., the new head v1 has d
1
✁■ By the I.H., d
1
✁CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
■ discovers every vertex v
■ properly assigns the d attribute of vertices, such that
✆■ finds a shortest path from s to any reachable vertex v: s to π
CPTR 430 Algorithms Graph Algorithms
■ Assume some vertex is assigned an incorrect d value
■ Let v be the vertex with a minimum δ
✆■ v
■ d
■ v is reachable from s; otherwise, δ
✆CPTR 430 Algorithms Graph Algorithms
■ Let u be the vertex immediately preceding v on a shortest path from s
■ δ
✆■ It follows from all of the above that
■ We will use these relationships to demonstrate a contradiction
CPTR 430 Algorithms Graph Algorithms
■ Suppose v is white ❚ The BFS algorithm sets d
■ Suppose v is black ❚ v has already been removed from the queue, so d
❚ Again, this contradicts d
CPTR 430 Algorithms Graph Algorithms
■ Suppose v is gray ❚ v was colored gray because its is adjacent to some other vertex, w, that
❚ Since it is adjacent to w, d
❚ d
■ Therefore,
✆■ π
CPTR 430 Algorithms Graph Algorithms
■ Search as deep as possible instead of visiting all neighboring vertices
■ The neighbors of the most recently discovered vertex are considered
■ BFS is queue based; DFS is stack based ■ The stack in maintained implicitly via recursion
CPTR 430 Algorithms Graph Algorithms
■ The DFS algorithm in your book uses the three colors (white, gray,
■ Two timestamps are used (d and f ): ❚ d is the time the vertex is discovered (grayed) ❚ f is the time the vertex is finished (backened)
CPTR 430 Algorithms Graph Algorithms
■ As in BFS, use aggregate analysis ■ DFS calls dfsVisit() exactly once for every vertex in the graph
■ dfsVisit() is invoked only on unmarked vertices (or white vertices)
CPTR 430 Algorithms Graph Algorithms
■ The loop in dfsVisit(v) scans the vertices adjacent to v; if adj
■ The total time for all executions of dfsVisit() is thus
v
■ DFS therefore takes Θ
✆■ The simpler DFS takes just Θ
✆CPTR 430 Algorithms Graph Algorithms
■ The intervals
■ The interval
■ The interval
CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
■ Tree edges—edges in the depth-first forest Gπ ■ Back edges—edges
✆■ Forward edges—non-tree edges
✆■ Cross edges—all other edges, e.g.: ❚ connect two vertices that where neither is an ancestor of the other ❚ connect vertices in different depth-first trees
CPTR 430 Algorithms Graph Algorithms
■ DFS can be modified to classify edge types as it proceeds ■ Color an edge
✆❚ white edge
❚ gray edge
❚ black edge
CPTR 430 Algorithms Graph Algorithms
■ Edges
✆■ The classification chosen is the one appropriate for the first edge
❚ If
✆❚ If
✆■ Undirected graphs contain no forward or cross edges
CPTR 430 Algorithms Graph Algorithms
■ Let
✆■ Suppose w.l.g. that d
■ Edge
✆■ Edge
✆CPTR 430 Algorithms Graph Algorithms
■ A linear ordering of all the vertices of a directed acyclic graph (dag)
■ In a topological sort you can arrange all the vertices in a line with all
■ Dags are useful for scheduling and prioritizing events ■ A topological sort allows proper ordering for single thread execution
CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
CPTR 430 Algorithms Graph Algorithms
■ Consider edge
■ v cannot be gray because ❚ v gray
☎■ v is white
☎■ v is black
☎❚ We are exploring from u
☎CPTR 430 Algorithms Graph Algorithms
■ DFS takes time O
✆■ Insertion onto the front of a linked list takes O
✆■ The total time for the topological sort is O
✆■ This is time linear in the number of edges and vertices
CPTR 430 Algorithms Graph Algorithms
■ A strongly connected component of a digraph G
❚ v is reachable from u and v is reachable from u ■ G
✂❚ G
✂❚ E
✂❚ Given an adjacency list representation of G, the time to create G
✂■ To compute the strongly connected components of digraph G, perform
❚ Running time: Θ
✆CPTR 430 Algorithms Graph Algorithms