TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 Content: • Basics, ADT Graph, Representations . [GT 13.1-2, LD 12.1, CL 22.1] • Graph Searching TTIT33 Algorithms and optimization [GT 13.3, LD 12.2, CL 22.2-3] – Depth-First Search – Breadth-First Search Algorithms Lecture 4 – Example graph problems solved with search Graphs Graphs HT 2006 • Directed Graphs 4.1 TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT 2006 4.2 TTIT33 Alorithms and Optimization – DALG Lecture 4 Basics Some 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]) Graphs HT 2006 - 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 4.3 TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT 2006 ADT Graph Representation of graphs (rough idea) Graph (V,E) with vertices {v1,…,vn} [Goodrich & Tamassia]: 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 represented as: • Adjacency matrix M[i,j] = 1 if {vi,vj} in E, and 0 otherwise • Adjacency list for each vi store a list of neighbors 2 4.5 1 2 3 4 5 1 4 Graphs HT 2006 4.4 TTIT33 Alorithms and Optimization – DALG Lecture 4 Defined differently by different authors. • • • • • • • • • • • [GT 13.4, LD 12.2, CL 22.4] 3 1 2 3 4 5 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 5 Graphs HT 2006 0 1 1 0 0 1 2 3 4 5 2 3 5 1 3 4 5 4.6 1 TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 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 Vertex objects augmented with integer keys w v 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 b a a 2 v w 0 2 0 b 0 List of edges Graphs HT 2006 4.7 TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT 2006 TTIT33 Alorithms and Optimization – DALG Lecture 4 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 2006 • 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} 4.9 Graphs HT 2006 TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 Example (notation of [G&T]) Example (cont.) unexplored vertex visited vertex unexplored edge discovery edge back edge A A A B A B D 4.8 E C A D E B A D E B C C C A A A B D E C Graphs HT 2006 4.10 B D E B C 4.11 D E D E C Graphs HT 2006 4.12 2 TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 Some applications of DFS Breadth First Search (BFS) • 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!! •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 Graphs HT 2006 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) 4.13 Graphs HT 2006 TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 Example Example (cont.) L0 unexplored vertex visited vertex unexplored edge discovery edge cross edge A A L0 L1 L1 A C E L1 D L1 C D E L1 4.15 D L1 F L0 C E © 2004 Goodrich, Tamassia D L1 Graphs HT 2006 TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 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 2006 C E L0 B L0 A B F A F © 2004 Goodrich, Tamassia D E L0 C L0 B A B 4.14 C E D F 4.16 •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 2006 4.17 Graphs HT 2006 4.18 3 TTIT33 Alorithms and Optimization – DALG Lecture 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 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 2006 4.19 © 2004 Goodrich, Tamassia TTIT33 Alorithms and Optimization – DALG Lecture 4 C G A D E B C A G* Graphs HT 2006 4.20 DAGs and Topological Ordering g c – If there’s a w not visited, print “no”. 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 Graphs HT 2006 4.21 TTIT33 Alorithms and Optimization – DALG Lecture 4 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’. © 2004 Goodrich, Tamassia E B TTIT33 Alorithms and Optimization – DALG Lecture 4 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 E B C A DAG G D B C A Graphs HT 2006 v4 E v5 v3 Topological ordering of G 4.22 TTIT33 Alorithms and Optimization – DALG Lecture 4 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 2006 4.23 © 2004 Goodrich, Tamassia Graphs HT 2006 4.24 4 TTIT33 Alorithms and Optimization – DALG Lecture 4 Topological Sorting Example 2 1 3 4 6 5 7 8 9 Graphs HT 2006 4.25 5