Presentation is loading. Please wait.

Presentation is loading. Please wait.

TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.1 TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.

Similar presentations


Presentation on theme: "TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.1 TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs."— Presentation transcript:

1 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.1 TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs

2 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.2 Content: Basics, ADT Graph, Representations Graph Searching –Depth-First Search –Breadth-First Search –Example graph problems solved with search Directed Graphs

3 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.3 Basics 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])

4 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.4 Terminology -End vertices of an edge -Edges incident on a vertex -Adjacent vertices (neighbors) -Degree of a vertex (undirected graph) -Indegree/outdegree (directed graph) -Path -Simple path -Cycle (loop)

5 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.5 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

6 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.6 ADT Graph Defined differently by different authors. [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

7 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.7 Representation of graphs (rough idea) Graph (V,E) with vertices {v 1,…,v n } represented as: Adjacency matrix M[i,j] = 1 if {v i,v j } in E, and 0 otherwise Adjacency list for each v i store a list of neighbors

8 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.8 The rough idea may need refinement 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]

9 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.9 Adjacency List Structure [G&T] Example graph ___________________ List of vertices Vertex objects Lists of incident edges Edge objects linked to respective vertices List of edges uvw ab a uv w b

10 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.10 Adjacency Matrix Structure [G&T] Example graph Vertex objects augmented with integer keys 2D-array adjacency array uvw ab 2 1 0 210  a uv w 01 2 b 1

11 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.11 Graph Searching 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)

12 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.12 DFS Algorithm 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}

13 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.13 Example (notation of [G&T]) DB A C ED B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

14 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.14 Example (cont.) DB A C E DB A C E DB A C E D B A C E

15 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.15 Some applications of DFS 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

16 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.16 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!! 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)

17 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.17 Example C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L0L0 L1L1 F CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F © 2004 Goodrich, Tamassia

18 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.18 Example (cont.) CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 © 2004 Goodrich, Tamassia

19 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.19 Example (cont.) CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 © 2004 Goodrich, Tamassia

20 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.20 Some applications of BFS 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 !

21 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.21 Directed Graphs 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)

22 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.22 Transitive Closure 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 B A D C E B A D C E G G* © 2004 Goodrich, Tamassia

23 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.23 Pick a vertex v in G. Perform a DFS from v in G. –If there’s a w not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. –If there’s a w not visited, print “no”. –Else, print “yes”. Running time: O(n+m). Strong Connectivity Algorithm G: G’: a d c b e f g a d c b e f g © 2004 Goodrich, Tamassia

24 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.24 DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1, …, v n of the vertices such that for every edge (v i, v j ), we have i  j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph admits a topological ordering if and only if it is a DAG B A D C E DAG G B A D C E Topological ordering of G v1v1 v2v2 v3v3 v4v4 v5v5 © 2004 Goodrich, Tamassia

25 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.25 Topological Sorting 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

26 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.26 Topological Sorting Example 9 © 2004 Goodrich, Tamassia

27 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.27 Topological Sorting Example 8 9 © 2004 Goodrich, Tamassia

28 TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.28 Topological Sorting Example 2 7 4 8 5 6 1 3 9


Download ppt "TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT 200611.1 TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs."

Similar presentations


Ads by Google