Graph Algorithms. Andreas Klappenecker

Similar documents
Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Shortest Path Algorithms

Outlines: Graphs Part-2

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

COT 6405 Introduction to Theory of Algorithms

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Basic Graph Algorithms (CLRS B.4-B.5, )

W4231: Analysis of Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

csci 210: Data Structures Graph Traversals

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Trees. Arash Rafiey. 20 October, 2015

Announcements Problem Set 4 is out!

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Review: Graph Theory and Representation

Algorithm Design and Analysis

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Elementary Graph Algorithms

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Graph Representation

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Strongly Connected Components. Andreas Klappenecker

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

3.1 Basic Definitions and Applications

22.3 Depth First Search

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Minimum Spanning Trees Ch 23 Traversing graphs

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

Minimum Spanning Trees

Graph: representation and traversal

(Re)Introduction to Graphs and Some Algorithms

CSC263 Week 8. Larry Zhang.

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

UNDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

Graph Algorithms. Definition

Graph representation

22.1 Representations of graphs

csci 210: Data Structures Graph Traversals

Breadth First Search. cse2011 section 13.3 of textbook

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Chapter 22 Elementary Graph Algorithms

CS 206 Introduction to Computer Science II

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

Trees Algorhyme by Radia Perlman

Week 12: Minimum Spanning trees and Shortest Paths

CSI 604 Elementary Graph Algorithms

Introduction to Graph Theory

Algorithm Design and Analysis

CS 561, Lecture 10. Jared Saia University of New Mexico

Graph traversal is a generalization of tree traversal except we have to keep track of the visited vertices.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Graph. Vertex. edge. Directed Graph. Undirected Graph

Paths, Circuits, and Connected Graphs

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Minimum Spanning Tree

Outline. Graphs. Divide and Conquer.

Representations of Graphs

CMSC 132: Object-Oriented Programming II

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Algorithm Design and Analysis

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Design and Analysis of Algorithms

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Unit 2: Algorithmic Graph Theory

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

DFS & STRONGLY CONNECTED COMPONENTS

Copyright 2000, Kevin Wayne 1

Shortest path problems

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Introduction to Graphs. common/notes/ppt/

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

CS 310 Advanced Data Structures and Algorithms

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

Basic Graph Definitions

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Shortest Path Problem

CS4800: Algorithms & Data Jonathan Ullman

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

Figure 1: A directed graph.

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Transcription:

Graph Algorithms Andreas Klappenecker

Graphs A graph is a set of vertices that are pairwise connected by edges. We distinguish between directed and undirected graphs. Why are we interested in graphs? Graphs are a very useful abstraction Graphs have many interesting applications Thousands of graph algorithms are known

Versatile Abstraction Application Vertices Edges Traffic Intersections Roads Social Network People Friendship Internet Class C network Connection Game Board Position Legal Move Erdos number People Coauthored Paper CMOS Circuits FET, Vdd, Vss, I/O Wires Financial Stock, Currency Transactions Programs Procedures Procedure Call f->g

The Internet

Undirected Graphs An undirected graph is a pair G=(V,E), where V is a finite set E is a subset of { e e V, e =2 }. The elements in V are called vertices. Elements in E are called edges, e.g. e={u,v}, written e=(u,v). Self-loops are not allowed for undirected graphs, e {u,u}={u}.

Directed Graphs An directed graph is a pair G=(V,E), where V is a finite set E is a subset of V x V The set of edges does not need to be symmetric. Thus, if (u,v) is an edge, then (v,u) does not need to be an edge. We illustrate a directed edge often by an arrow u -> v.

Graph Terminology If e=(u,v) is an edge in a graph, then v is called adjacent to u. For undirected graphs, adjacency is a symmetric relation. The edge e is said to be incident to u and v. The number of edges incident to a vertex is called the degree of the vertex.

Graph Terminology A path is a sequence of vertices that are connected by edges. A cycle is a path whose first and last vertices are the same. Two vertices are connected if and only if there is a path between them.! vertex of degree 3 path cycle

Breadth-First Search

Breadth First Search (BFS) Input: A graph G = (V,E) and source node s in V mark all nodes v in V as unvisited mark source node s as visited enq(q,s) // first-in first-out queue Q while (Q is not empty) { u := deq(q); for each unvisited neighbor v of u { mark v as visited; enq(q,v); } }

demo-bfs Example

BFS Example s a b Visit the nodes in the order: s c a, d d b, c

BFS Tree We can make a spanning tree rooted at the source node s by remembering the parent of each node. 12

Breadth First Search (BFS) Input: A graph G = (V,E) and source node s in V mark all nodes v in V as unvisited; set parent[v] := nil for all v in V mark source node s as visited; parent[s] := s; enq(q,s) // first-in first-out queue Q while (Q is not empty) { u := deq(q); for each unvisited neighbor v of u { mark v as visited; enq(q,v); parent[v] := u } }

BFS Tree Example s c a b d 15

BFS Trees The BFS tree is in general not unique for a given graph. It depends on the order in which neighboring nodes are processed. 16

BFS Numbering During the breadth-first search, assign to each node v its distance d[v] from the source. 17

Breadth First Search (BFS) Input: A graph G = (V,E) and source node s in V mark all nodes v in V as unvisited; set parent[v] := nil; d[v] = for all v in V mark source node s as visited; parent[s] := s; d[v] = 0 enq(q,s) // first-in first-out queue Q while (Q is not empty) { u := deq(q); for each unvisited neighbor v of u { mark v as visited; enq(q,v); parent[v] := u; d[v] = d[u]+1 } }

BFS Numbering Example d = 1 a b d = 2 d = 0 s c d = 2 d d = 1 20

Shortest Path Tree Theorem: The BFS algorithm visits all and only nodes reachable from s for all nodes v sets d[v] to the shortest path distance from s to v sets parent variables to form a shortest path tree 21

Proof Ideas We use induction on the distance from the source node s to show that a node v at distance x from s has has correct d[v]. Basis: Distance 0. d[s] is set to 0. Induction: Assume that all nodes u at distance x-1 from s satisfy d[u]=x-1. Our goal is to show that every node v at distance x satisfies d[v]=x as well. Since v is at distance x, it has at least one neighbor at distance x-1. Let u be the first of these neighbors that is enqueued. 22

Proof Ideas dist=x-1 c dist=x+1 s u v dist=x d dist=x-1 A key property of shortest path distances: If v has distance x, it must have a neighbor with distance x-1, no neighbor has distance less than x-1, and no neighbor has distance more than x+1 23

Proof Ideas Claim: When the node u is dequeued, then v is still unvisited. Indeed, this follows from behavior of the queue and the fact that d never underestimates the distance. By induction, d[u] = x-1. When v is enqueued, d[v] is set to d[u] + 1 = x. 24

BFS Running Time Initialization of each node takes O(V) time Every node is enqueued once and dequeued once, taking O(V) time When a node is dequeued, all its neighbors are checked to see if they are unvisited, taking time proportional to number of neighbors of the node, and summing to O(E) over all iterations Total time is O(V+E) 25

Credits! In the preparation of these slides, I got inspired by slides by Robert Sedgewick. The slides on BFS are based on slides by Jennifer Welch.