CSE 2331/5331 Topic 9: Basic Graph Alg.

Slides:



Advertisements
Similar presentations
CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Topological Sorting and Least-cost Path Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
Introduction to Algorithms
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Topological Sorting.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
CSC317 Graph algorithms Why bother?
Topological Sort Minimum Spanning Tree
CSC 413/513: Intro to Algorithms
Depth-First Search.
CS200: Algorithm Analysis
Graph Search Algorithms
Graph: representation and traversal CISC4080, Computer Algorithms
CSCE 411 Design and Analysis of Algorithms
Graph.
CSE 421: Introduction to Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph & BFS.
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Advanced Algorithms Analysis and Design
Elementary graph algorithms Chapter 22
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Analysis of Algorithms CS 477/677
Chapter 11 Graphs.
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Algorithms Searching in a Graph.
Graphs Chapter 7 Visit for more Learning Resources.
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
Elementary graph algorithms Chapter 22
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
3.2 Graph Traversal.
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

CSE 2331/5331 Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

What Is A Graph Graph G = (V, E) V: set of nodes E: set of edges b a f c d e Example: V ={ a, b, c, d, e, f } E ={(a, b), (a, d), (a, e), (b, c), (b, d), (b, e), (c, e), (e,f)} CSE 2331/5331

Un-directed graph Directed graph 𝐸≤ 𝑉 2 𝐸≤ 𝑉 2 b a f c d e 𝐸≤ 𝑉 2 Directed graph 𝐸≤ 𝑉 2 b a f c d e CSE 2331/5331

Un-directed graphs CSE 2331/5331

Vertex Degree deg(v) = degree of v = # edges incident on v b a f c d e Lemma: 𝑣 𝑖 ∈𝑉(𝐺) deg 𝑣 𝑖 = 2|𝐸| CSE 2331/5331

Some Special Graphs Complete graph Path Cycle Planar graph Tree CSE 2331/5331

Representations of Graphs Adjacency lists Each vertex u has a list, recording its neighbors i.e., all v’s such that (u, v)  E An array of V lists V[i].degree = size of adj list for node 𝑣 𝑖 V[i].AdjList = adjacency list for node 𝑣 𝑖 CSE 2331/5331

Adjacency Lists For vertex v  V, its adjacency list has size: deg(v) decide whether (v, u)  E or not in time Θ (deg(v)) Size of data structure (space complexity): Θ(|V| + |E|) = Θ (V+E) CSE 2331/5331

Adjacency Matrix 𝑉×𝑉 matrix 𝐴 𝐴 𝑖,𝑗 =1 if 𝑣 𝑖 , 𝑣 𝑗 is an edge Otherwise, 𝐴 𝑖,𝑗 =0 CSE 2331/5331

Adjacency Matrix Size of data structure: Θ ( V  V) Time to determine if (v, u)  E : Θ(1) Though larger, it is simpler compared to adjacency list. CSE 2331/5331

Sample Graph Algorithm Input: Graph G represented by adjacency lists Running time: Θ(V + E) CSE 2331/5331

Connectivity A path in a graph is a sequence of vertices 𝑢 1 , 𝑢 2 ,…, 𝑢 𝑘 such that there is an edge 𝑢 𝑖 , 𝑢 𝑖+1 between any two adjacent vertices in the sequence Two vertices 𝑢, 𝑤∈𝑉 𝐺 are connected if there is a path in G from u to w. We also say that w is reachable from u. A graph G is connected if every pair of nodes 𝑢, 𝑤∈𝑉 𝐺 are connected. CSE 2331/5331

Connectivity Checking How to check if the graph is connected? One approach: Graph traversal BFS: breadth-first search DFS: depth-first search CSE 2331/5331

BFS: Breadth-first search Input: Given graph G = (V, E), and a source node s  V Output: Will visit all nodes in V reachable from s For each 𝑣∈𝑉, output a value 𝑣.𝑑 𝑣.𝑑= distance (smallest # of edges) from s to v. 𝑣.𝑑= ∞ if v is not reachable from s. CSE 2331/5331

Intuition Starting from source node s, Need a data-structure Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s … Need a data-structure to store nodes to be explored. CSE 2331/5331

Intuition cont. A node can be: 𝑣.𝑑 : un-discovered discovered, but not explored explored (finished) 𝑣.𝑑 : is set when node v is first discovered. Need a data structure to store discovered but un-explored nodes FIFO ! Queue CSE 2331/5331

Pseudo-code Use adjacency list representation Time complexity: Θ(V+E) CSE 2331/5331

Correctness of Algorithm A node not reachable from s will not be visited A node reachable from s will be visited 𝑣.𝑑 computed is correct: Intuitively, if all nodes k distance away from s are in level k, and no other nodes are in level k, Then all nodes (k+1)-distance away from s must be in level (k+1). Rigorous proof by induction Consider the breadth-first tree generated by algorithm: Any node u from k’th level: d[u] = k Claim: all nodes v with  (s, v) = k are in level k and all nodes from level k has  (s, v) = k CSE 2331/5331

BFS tree A node 𝑣 is the parent of 𝑢 if A BFS tree 𝑇 𝑢 was first discovered when exploring 𝑣 A BFS tree 𝑇 Root: source node 𝑠 Nodes in level 𝑘 of 𝑇 are distance 𝑘 away from 𝑠 CSE 2331/5331

Connectivity-Checking Time complexity: Θ(V+E) CSE 2331/5331

Summary for BFS Starting from source node s, visits remaining nodes of graph from small distance to large distance This is one way to traverse an input graph With some special property where nodes are visited in non-decreasing distance to the source node s. Return distance between s to any reachable node in time Θ (|V| + |E|) CSE 2331/5331

DFS: Depth-First Search Another graph traversal algorithm BFS: Go as broad as possible in the algorithm DFS: Go as deep as possible in the algorithm CSE 2331/5331

Example Perform DFS starting from 𝑣 1 What if we add edge 𝑣 1 , 𝑣 8 CSE 2331/5331

DFS Time complexity Θ(V+E) CSE 2331/5331 Intuitively, instead of using a FIFO Queue, now we use a LIFO stack. Time complexity Θ(V+E) CSE 2331/5331

Depth First Search Tree If v is discovered when exploring u Set 𝑣.𝑝𝑎𝑟𝑒𝑛𝑡=𝑢 The collection of edges 𝑣.𝑝𝑎𝑟𝑒𝑛𝑡, 𝑣 form a tree, called Depth-first search tree. CSE 2331/5331

DFS with DFS Tree CSE 2331/5331

Example Perform DFS starting from 𝑣 1 CSE 2331/5331

Another Connectivity Test CSE 2331/5331

Traverse Entire Graph CSE 2331/5331

Remarks DFS(G, k) Same time complexity as BFS Another way to compute all nodes reachable to the node 𝑣 𝑘 Same time complexity as BFS There are nice properties of DFS and DFS tree that we are not reviewing in this class. CSE 2331/5331

Directed Graphs CSE 2331/5331

Un-directed graph Directed graph 𝐸≤ 𝑉 2 𝐸≤ 𝑉 2 Directed graph Each edge 𝑢,𝑣 is directed from u to v 𝐸≤ 𝑉 2 b a f c d e CSE 2331/5331

Vertex Degree indeg(v) = # edges of the form 𝑢, 𝑣 outdeg(v) = # edges of the form 𝑣, 𝑢 b a f c d e Lemma: 𝑣 𝑖 ∈𝑉(𝐺) indeg 𝑣 𝑖 = |𝐸| Lemma: 𝑣 𝑖 ∈𝑉(𝐺) outdeg 𝑣 𝑖 = |𝐸| CSE 2331/5331

Representations of Graphs Adjacency lists Each vertex u has a list, recording its neighbors i.e., all v’s such that (u, v)  E An array of V lists V[i].degree = size of adj list for node 𝑣 𝑖 V[i].AdjList = adjacency list for node 𝑣 𝑖 CSE 2331/5331

Adjacency Lists For vertex v  V, its adjacency list has size: outdeg(v) decide whether (v, u)  E or not in time O(outdeg(v)) Size of data structure (space complexity): Θ(V+E) CSE 2331/5331

Adjacency Matrix 𝑉×𝑉 matrix 𝐴 𝐴 𝑖,𝑗 =1 if 𝑣 𝑖 , 𝑣 𝑗 is an edge Otherwise, 𝐴 𝑖,𝑗 =0 CSE 2331/5331

Adjacency Matrix Size of data structure: Θ ( V  V) Time to determine if (v, u)  E : O(1) Though larger, it is simpler compared to adjacency list. CSE 2331/5331

Sample Graph Algorithm Input: Directed graph G represented by adjacency list Running time: O(V + E) CSE 2331/5331

Connectivity A path in a graph is a sequence of vertices 𝑢 1 , 𝑢 2 ,…, 𝑢 𝑘 such that there is an edge 𝑢 𝑖 , 𝑢 𝑖+1 between any two adjacent vertices in the sequence Given two vertices 𝑢, 𝑤∈𝑉 𝐺 , we say that w is reachable from u if there is a path in G from u to w. Note: w is reachable from u DOES NOT necessarily mean that u is reachable from w. CSE 2331/5331

Reachability Test How many (or which) vertices are reachable from a source node, say 𝑣 1 ? CSE 2331/5331

BFS and DFS The algorithms for BFS and DFS remain the same Each edge is now understood as a directed edge BFS(V,E, s) : visits all nodes reachable from s in non-decreasing order CSE 2331/5331

BFS Starting from source node s, Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s … CSE 2331/5331

Pseudo-code Use adjacency list representation Time complexity: Θ(V+E) CSE 2331/5331

Number of Reachable Nodes Compute # nodes reachable from 𝑣 𝑘 Time complexity: Θ(V+E) CSE 2331/5331

DFS: Depth-First Search Similarly, DFS remains the same Each edge is now a directed edge If we start with all nodes unvisited, Then DFS(G, 𝑘) visits all nodes reachable to node 𝑣 𝑘 BFS from previous NumReachable() procedure can be replaced with DFS. CSE 2331/5331

More Example Is 𝑣 1 reachable from 𝑣 12 ? Is 𝑣 12 reachable from 𝑣 1 ? CSE 2331/5331

DFS above can be replaced with BFS DFS(G, k); DFS above can be replaced with BFS CSE 2331/5331

Topological Sort CSE 2331/5331

Directed Acyclic Graph A directed cycle is a sequence 𝑢 1 , 𝑢 2 ,…, 𝑢 𝑘 , 𝑢 1 such that there is a directed edge between any two consecutive nodes. DAG: directed acyclic graph Is a directed graph with no directed cycles. CSE 2331/5331

Topological Sort A topological sort of a DAG G = (V, E) A linear ordering A of all vertices from V If edge (u,v)  E => A[u] < A[v] undershorts pants belt shirt tie jacket shoes socks watch CSE 2331/5331

Another Example Why requires DAG? Is the sorting order unique? CSE 2331/5331

A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG). CSE 2331/5331

Question How to topologically sort a given DAG? Intuition: Which node can be the first node in the topological sort order? A node with in-degree 0 ! After we remove this, the process can be repeated. CSE 2331/5331

Example undershorts pants belt shirt tie jacket shoes socks watch CSE 2331/5331

CSE 2331/5331

Topological Sort – Simplified Implementation CSE 2331/5331

Time complexity Correctness: Θ(V+E) What if the algorithm terminates before we finish visiting all nodes? Procedure TopologicalSort(G) outputs a sorted list of all nodes if and only if the input graph G is a DAG If G is not DAG, the algorithm outputs only a partial list of vertices. CSE 2331/5331

Remarks Other topological sort algorithm by using properties of DFS CSE 2331/5331

Last Analyzing graph algorithms Adjacency list representation or Adjacency matrix representation CSE 2331 / 5331

Edge-weighted un-directed graph G = (V, E) and edge weight function 𝑤:𝐸→𝑅 E.g, road network, where each node is a city, and each edge is a road, and weight is the length of this road. 3 5 b a f c d e 1 1 2 1 1 2 4 CSE 2331 / 5331

Example 1 Assume G is represented by adjacency list Q is priority-queue implemented by min-heap CSE 2331 / 5331

Example 2 Assume G is represented by adjacency matrix What if move line 10 to above line 8? What if move line 10 to above line 9? CSE 2331 / 5331