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

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graph & BFS.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
1 The Graph Abstract Data Type CS 5050 Chapter 6.
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive.
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
Chapter 6 Graphs ORD DFW SFO LAX
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Spring 2007Graphs1 ORD DFW SFO LAX
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Depth-First Search Lecture 21: Graph Traversals
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSC401: Analysis of Algorithms 6-1 CSC401 – Analysis of Algorithms Chapter 6 Graphs Objectives: Introduce graphs and data structures Discuss the graph.
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
Graphs and Paths : Chapter 15 Saurav Karmakar
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
1 COMP9024: Data Structures and Algorithms Week Eleven: Graphs (I) Hui Wu Session 1, 2016
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§12.4) Reachability (§12.4.1) Directed DFS Strong connectivity Transitive.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Graphs.
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Chapter 14 Graph Algorithms
Directed Graphs 9/20/2018 1:45 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Directed Graphs 5/1/15 12:25:22 PM
Graph Algorithms Using Depth First Search
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Graphs.
Graphs.
Graphs.
Graphs CSE 2011 Winter November 2018.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Graphs Part 1 ORD SFO LAX DFW
Graphs G = (V, E) V are the vertices; E are the edges.
Elementary Graph Algorithms
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT Topological Sorting Example