computer science engineering 423 823
play

Computer Science & Engineering 423/823 Introduction Design and - PowerPoint PPT Presentation

CSCE423/823 Computer Science & Engineering 423/823 Introduction Design and Analysis of Algorithms Types of Graphs Lecture 03 Elementary Graph Algorithms (Chapter 22) Representations of Graphs Elementary Graph Algorithms Stephen


  1. CSCE423/823 Computer Science & Engineering 423/823 Introduction Design and Analysis of Algorithms Types of Graphs Lecture 03 — Elementary Graph Algorithms (Chapter 22) Representations of Graphs Elementary Graph Algorithms Stephen Scott Applications (Adapted from Vinodchandran N. Variyam) Spring 2010 1 / 29

  2. Introduction CSCE423/823 Introduction Graphs are abstract data types that are applicable to numerous Types of Graphs problems Representations Can capture entities , relationships between them, the degree of the of Graphs relationship, etc. Elementary Graph This chapter covers basics in graph theory, including representation, Algorithms and algorithms for basic graph-theoretic problems Applications We’ll build on these later this semester 2 / 29

  3. Types of Graphs CSCE423/823 A (simple, or undirected) graph G = ( V, E ) consists of V , a Introduction nonempty set of vertices and E a set of unordered pairs of distinct Types of Graphs vertices called edges Representations of Graphs E D V={A,B,C,D,E} Elementary Graph Algorithms E={ (A,D),(A,E),(B,D), Applications (B,E),(C,D),(C,E)} B A C 3 / 29

  4. Types of Graphs (2) CSCE423/823 A directed graph (digraph) G = ( V, E ) consists of V , a nonempty set of vertices and E a set of ordered pairs of distinct vertices called Introduction edges Types of Graphs Representations of Graphs Elementary Graph Algorithms Applications 4 / 29

  5. Types of Graphs (3) CSCE423/823 A weighted graph is an undirected or directed graph with the additional property that each edge e has associated with it a real Introduction number w ( e ) called its weight Types of 3 Graphs Representations of Graphs 12 Elementary Graph 0 Algorithms -6 7 Applications 4 3 Other variations: multigraphs, pseudographs, etc. 5 / 29

  6. Representations of Graphs CSCE423/823 Introduction Types of Graphs Two common ways of representing a graph: Adjacency list and Representations of Graphs adjacency matrix Adjacency List Adjacency Matrix Let G = ( V, E ) be a graph with n vertices and m edges Elementary Graph Algorithms Applications 6 / 29

  7. Adjacency List CSCE423/823 For each vertex v ∈ V , store a list of vertices adjacent to v Introduction For weighted graphs, add information to each node Types of Graphs How much is space required for storage? Representations of Graphs a b a b c d Adjacency List Adjacency Matrix b a e Elementary Graph Algorithms c c a d c Applications d a c e e b c d d e 7 / 29

  8. Adjacency Matrix CSCE423/823 Use an n × n matrix M , where M ( i, j ) = 1 if ( i, j ) is an edge, 0 otherwise Introduction If G weighted, store weights in the matrix, using ∞ for non-edges Types of Graphs How much is space required for storage? Representations of Graphs Adjacency List a b Adjacency Matrix a a b c d e Elementary 0 1 1 1 0 Graph Algorithms b 1 0 0 0 1 c Applications c 1 0 0 1 1 d 1 0 1 0 1 e 0 1 1 1 0 d e 8 / 29

  9. Breadth-First Search (BFS) CSCE423/823 Given a graph G = ( V, E ) (directed or undirected) and a source node Introduction s ∈ V , BFS systematically visits every vertex that is reachable from s Types of Graphs Uses a queue data structure to search in a breadth-first manner Representations of Graphs Creates a structure called a BFS tree such that for each vertex Elementary v ∈ V , the distance (number of edges) from s to v in tree is the Graph Algorithms shortest path in G Breadth-First Search Initialize each node’s color to white Depth-First Search As a node is visited, color it to gray ( ⇒ in queue), then black ( ⇒ Applications finished) 9 / 29

  10. BFS Algorithm CSCE423/823 1 for each vertex u ∈ V \ { s } do 2 color [ u ] = white 3 d [ u ] = ∞ Introduction 4 π [ u ] = nil 5 end Types of 6 Graphs color [ s ] = gray 7 d [ s ] = 0 Representations 8 π [ s ] = nil of Graphs 9 Q = ∅ Elementary 10 Enqueue ( Q, s ) Graph 11 while Q � = ∅ do Algorithms 12 u = Dequeue ( Q ) Breadth-First 13 for each v ∈ Adj [ u ] do Search 14 if color [ v ] == white then Depth-First 15 Search color [ v ] = gray 16 d [ v ] = d [ u ] + 1 Applications 17 π [ v ] = u 18 Enqueue ( Q, v ) 19 end 20 end 21 color [ u ] = black 22 end 10 / 29 Algorithm 1: BFS( G, s )

  11. BFS Example s33 22.2 Breadth-first search CSCE423/823 Introduction ffi ffi o o Types of 0 tl Graphs Representations of Graphs Elementary ffirffiffiffifl fffi:$ffiiflffiml o o Graph r22 222 Algorithms Breadth-First Search Depth-First Search Applications ffi!gffifi]$ffifrl flffiffiffiflffifr o n zzJ ZJJ ffi o |l:ilfql*l o 11 / 29 l::ld::dj;Ii:;l 3 JJ oa Figure 22.3 The operation of BFS on an undirected graph. Tree edges are shown shaded as they are produced by BFS. Within each vertex u is shown dful. The queue Q is shown at the beginning of each iteration of the while loop of lines 10-18. Vertex distances are shown next to vertices in the queu: Figure 22.3 illuslrates the progress of BFS on a sample graph. The procedure BFS works as follows. With the exception of the source vertex r, lines 1-4 paint every vertex white, set dlul to be infinity for each vertex u, and set vertex to be NIL. Line 5 paints s gray, since the parent of every it is considered to be discovered when the procedure begins. Line 6 initializes d[s] to 0, and line 7 sets the predecessor of the source to be NII-. Lines 8-9 initialize Q to the queue just the vertex containing s.

  12. s33 22.2 Breadth-first search ffi ffi o o 0 tl ffirffiffiffifl fffi:$ffiiflffiml o o r22 222 BFS Example (2) ffi!gffifi]$ffifrl flffiffiffiflffifr o n CSCE423/823 zzJ ZJJ Introduction Types of ffi Graphs o |l:ilfql*l o l::ld::dj;Ii:;l Representations 3 JJ of Graphs Elementary Graph Algorithms Breadth-First oa Search Depth-First Search Applications graph. Tree edges Figure 22.3 The operation of BFS on an undirected are shown shaded as they produced vertex u is shown dful. The queue Q is shown at the beginning are by BFS. Within each iteration of the while loop of lines 10-18. Vertex distances are shown next to vertices in the of each queu: 12 / 29 Figure 22.3 illuslrates the progress of BFS on a sample graph. The procedure BFS works as follows. With the exception of the source vertex r, lines 1-4 paint every vertex white, set dlul to be infinity for each vertex u, and set the parent of every vertex to be NIL. Line 5 paints s gray, since it is considered to be discovered when the procedure begins. Line 6 initializes d[s] to 0, and line 7 the predecessor to be NII-. Lines 8-9 initialize Q to the queue sets of the source just the vertex containing s.

  13. BFS Properties CSCE423/823 Introduction What is the running time? Types of Hint: How many times will a node be enqueued? Graphs After the end of the algorithm, d [ v ] = shortest distance from s to v Representations of Graphs ⇒ Solves unweighted shortest paths Elementary Can print the path from s to v by recursively following π [ v ] , π [ π [ v ]] , Graph Algorithms etc. Breadth-First Search If d [ v ] == ∞ , then v not reachable from s Depth-First Search ⇒ Solves reachability Applications 13 / 29

  14. Depth-First Search (DFS) CSCE423/823 Introduction Another graph traversal algorithm Types of Graphs Unlike BFS, this one follows a path as deep as possible before Representations of Graphs backtracking Elementary Where BFS is “queue-like,” DFS is “stack-like” Graph Algorithms Tracks both “discovery time” and “finishing time” of each node, Breadth-First Search Depth-First which will come in handy later Search Applications 14 / 29

  15. DFS Algorithm CSCE423/823 for each vertex u ∈ V do 1 Introduction color [ u ] = white 2 Types of π [ u ] = nil 3 Graphs Representations end 4 of Graphs time = 0 5 Elementary Graph for each vertex u ∈ V do 6 Algorithms if color [ u ] == white then Breadth-First 7 Search DFS-Visit ( u ) Depth-First 8 Search end 9 Applications end 10 Algorithm 2: DFS( G ) 15 / 29

  16. DFS Algorithm (2) CSCE423/823 color [ u ] = gray 1 time = time + 1 Introduction 2 Types of d [ u ] = time 3 Graphs for each v ∈ Adj [ u ] do 4 Representations if color [ v ] == white then of Graphs 5 π [ v ] = u Elementary 6 Graph DFS-Visit ( v ) Algorithms 7 Breadth-First Search end 8 Depth-First Search end 9 Applications color [ u ] = black 10 f [ u ] = time = time + 1 11 Algorithm 3: DFS-Visit( u ) 16 / 29

Recommend


More recommend