TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Content: • Basics, ADT Graph, Representations • Graph Searching TDDB56 – DALGOPT-D Algorithms and optimization – Depth-First Search – Breadth-First Search Lecture 11 & 12 – Example graph problems solved with search Graphs Graphs HT 2005 • Directed Graphs 11.1 Graphs HT 2005 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Basics Terminology • Graph G = (V,E) : – V a set of vertices; – E a set of pairs of vertices, called edges • Directed graph: the edges are ordered pairs • Undirected graph: the edges are unordered pairs – Edges and vertices may be labelled by additional info Example: A graph with - vertices representing airports; info airport code - edges representing flight routes; info mileage (see [G&T]) - End vertices of an edge Edges incident on a vertex Adjacent vertices (neighbors) Degree of a vertex (undirected graph) Indegree/outdegree (directed graph) 2 Path 1 3 Simple path Cycle (loop) 4 5 Path: <1 5 3 2 1 4> Simple path: <1 4> Cycle: <1 5 4 1 2 3 5 1> Simple cycle: <1 5 4 1> = < 5 4 1 5> =... 2 2 1 4 Graphs HT 2005 11.2 11.3 Graphs HT 2005 3 5 1 4 3 5 11.4 1 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs ADT Graph Terminology cont… - G is connected iff there is a path between any two vertices - G is a (free) tree iff there is a unique simple path between any two vertices; notice: root is not distinguished - G is complete iff any two vertices are connected by an edge - G´ = (V´, E´) is a subgraph of G = (V,E) iff V´⊆ V and E´⊆ E, - A connected component of G is any maximal subgraph of G which is connected Graphs HT 2005 11.5 TDDB56 DALGOPT-D – Lecture 11 – Graphs • Adjacency matrix M[i,j] = 1 if {vi,vj} in E, and 0 otherwise • Adjacency list for each vi store a list of neighbors 1 2 3 4 5 1 4 3 0 0 0 1 0 • • • • • • • • • • • endVertices(e): an array of the two endvertices of e opposite(v, e): the vertex opposite of v on e areAdjacent(v, w): true iff v and w are adjacent replace(v, x): replace element at vertex v with x replace(e, x): replace element at edge e with x insertVertex(o): insert a vertex storing element o insertEdge(v, w, o): insert an edge (v,w) storing element o removeVertex(v): remove vertex v (and its incident edges) removeEdge(e): remove edge e incidentEdges(v): edges incident to v vertices(): all vertices in the graph; edges(): all edges in the graph Graphs HT 2005 11.6 The rough idea may need refinement represented as: 1 2 3 4 5 [Goodrich & Tamassia]: TDDB56 DALGOPT-D – Lecture 11 – Graphs Representation of graphs (rough idea) Graph (V,E) with vertices {v1,…,vn} 2 Defined differently by different authors. 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 5 Graphs HT 2005 0 1 1 0 0 1 2 3 4 5 2 3 5 1 3 4 5 11.7 • Vertices and edges stored as cells: may contain additional information • V and E are sets: use a set representation (list, table, dictionary?) • Incident edges of a vertex should be accessible very efficiently. • Neighbors of a vertex should be accessible very efficiently. For refined variants of Adjacency list and Adjacency matrix see [G&T] Graphs HT 2005 11.8 2 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Adjacency List Structure [G&T] Adjacency Matrix Structure [G&T] Example graph ___________________ a v b u v a Example graph w b u w List of vertices Vertex objects u v w Vertex objects augmented with integer keys 0 u 1 Lists of incident edges 0 2D-array adjacency array 0 1 2 0 1 0 1 Edge objects linked to respective vertices a b a 2 2 v w 0 0 0 b List of edges Graphs HT 2005 11.9 Graphs HT 2005 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Graph Searching DFS Algorithm The problem : systematically visit the vertices of a graph reachable by edges from a given vertex. Numerous applications: - robotics: routing, motion planning - solving optimization problems (see OPT part) Graph Searching Techniques: - Depth First Search (DFS) - Breadth First Search (BFS) Graphs HT 2005 11.11 11.10 • Input: a graph G and a vertex s • Visits all vertices connected with s in time O(|V|+|E|) procedure DepthFirstSearch(G =(V,E), s): for each v in V do explored(v) ← false; RDFS(G,s) procedure RDFS(G,s): explored(s) ← true; previsit(s) {some operation on s before visiting its neighbors} for each neighbor t of s if not explored(t) then RDFS(G,t) postvisit(s) {some operation on s after visiting its neighbors} Graphs HT 2005 11.12 3 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Example (notation of [G&T]) Example (cont.) unexplored vertex visited vertex unexplored edge discovery edge back edge A A A B A B D E C A D E D B E C C C A A A B D E C Graphs HT 2005 B A B D E B C 11.13 TDDB56 DALGOPT-D – Lecture 11 – Graphs Some applications of DFS Breadth First Search (BFS) Graphs HT 2005 11.15 E D E C Graphs HT 2005 TDDB56 DALGOPT-D – Lecture 11 – Graphs •Checking if G is connected •Finding connected components of G •Finding a path between vertices •Checking acyclicity/finding a cycle •Finding a spanning forest of G D 11.14 • Input: a graph G and a vertex s • Visits all vertices connected with s in time O(|V|+|E|) in order of increasing distance to s • Apply Queue!! procedure BFS (G=(V,E), s): for each v in V do explored(v) ← false; S ← MakeEmptyQueue(); Enqueue(S,s); explored(s) ← true; while not IsEmpty(S) do t ← Dequeue(S) visit(t) for each neighbor v of t do if not explored(v) then explored(v) ← true; Enqueue(S,v) Graphs HT 2005 11.16 4 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Example Example (cont.) L0 unexplored vertex visited vertex unexplored edge discovery edge cross edge A A L0 L1 L1 C E L1 D L1 C D E L1 11.17 D L1 F L0 C E © 2004 Goodrich, Tamassia D L1 Graphs HT 2005 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Example (cont.) Some applications of BFS L0 L1 B L2 L0 L1 C E D L1 B L2 F A C E D F A B L2 L0 A C E © 2004 Goodrich, Tamassia D C E D F A B L2 F A B L2 A B L2 F Graphs HT 2005 C E L0 B L0 A B F A F © 2004 Goodrich, Tamassia D E L0 C L0 B A B A C E D F 11.18 •Checking if G is connected •Finding connected components of G •Finding a path of minimal length between vertices •Checking acyclicity/finding a cycle •Finding a spanning forest of G Compare with DFS ! F Graphs HT 2005 11.19 Graphs HT 2005 11.20 5 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Directed Graphs Transitive Closure • Digraphs: edges are ordered pairs. • Digraphs have many applications, like – Task scheduling – Route planning, ….. • Search algorithms apply, follow directions. • DFS applications for digraphs: – Transitive closure but in O(n(m+n)) – Checking strong connectivity – Topological sort of a directed acyclic graph DAG (scheduling) Graphs HT 2005 11.21 © 2004 Goodrich, Tamassia TDDB56 DALGOPT-D – Lecture 11 – Graphs C G A D E B C A G* Graphs HT 2005 11.22 g c d e b f – If there’s a w not visited, print “no”. – Else, print “yes”. a G’: g c d • Running time: O(n+m). e b f 11.23 A directed acyclic graph (DAG) is a digraph that has no directed cycles • A topological ordering of a digraph is a numbering v1 , …, vn of the vertices such that for every edge (vi , vj), we have i < j • Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the v2 precedence constraints Theorem A digraph admits a topological v1 ordering if and only if it is a DAG D • a G: • Let G’ be G with edges reversed. • Perform a DFS from v in G’. Graphs HT 2005 B DAGs and Topological Ordering – If there’s a w not visited, print “no”. © 2004 Goodrich, Tamassia E TDDB56 DALGOPT-D – Lecture 11 – Graphs Strong Connectivity Algorithm • Pick a vertex v in G. • Perform a DFS from v in G. D • Given a digraph G, the transitive closure of G is the digraph G* such that – G* has the same vertices as G – if G has a directed path from u to v (u ≠ v), G* has a directed edge from u to v • The transitive closure provides reachability information about a digraph © 2004 Goodrich, Tamassia Graphs HT 2005 E B C A DAG G D B C A v4 E v5 v3 Topological ordering of G 11.24 6 TDDB56 DALGOPT-D – Lecture 11 – Graphs TDDB56 DALGOPT-D – Lecture 11 – Graphs Topological Sorting Topological Sorting Example Use modified DFS! procedure TopologicalSort(G): nextnumber ← |G| for each vertex v in G do explored(v) ← false; for each vertex v in G do if not explored(v) then RDFS(G,v) procedure RDFS(G,s): explored(s) ← true; for each neighbor t of s if not explored(t) then RDFS(G,t) number(s) ←nextnumber; nextnumber ←nextnumber-1 Graphs HT 2005 11.25 TDDB56 DALGOPT-D – Lecture 11 – Graphs © 2004 Goodrich, Tamassia Graphs HT 2005 11.26 TDDB56 DALGOPT-D – Lecture 11 – Graphs Topological Sorting Example Topological Sorting Example 8 9 © 2004 Goodrich, Tamassia Graphs HT 2005 9 11.27 © 2004 Goodrich, Tamassia Graphs HT 2005 11.28 7 TDDB56 DALGOPT-D – Lecture 11 – Graphs Topological Sorting Example 2 1 3 4 6 5 7 8 9 Graphs HT 2005 11.29 8