What is a graph ? 1 2 3 4 5. G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices 1 2 3 4 5.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
CS203 Lecture 15.
CS38 Introduction to Algorithms Lecture 2 April 3, 2014.
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
Connected Components, Directed Graphs, Topological Sort COMP171.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
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.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
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.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Topological Sort: Definition
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
Lecture 7 Graph Traversal
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.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Introduction to Algorithms
Breadth-First Search (BFS)
Chapter 22 Elementary Graph Algorithms
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.
Lecture 12 Graph Algorithms
Graph Algorithms Using Depth First Search
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Richard Anderson Autumn 2016 Lecture 5
Lecture 6 Graph Traversal
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 16 1 – Graphs Graph Categories Strong Components
Important Problem Types and Fundamental Data Structures
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
Heaps Chapter 6 Section 6.9.
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

What is a graph ?

G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices

What is a graph ? G=(V,E) V = {1,2,3,4,5} E = {{1,5}, {3,5}, {2,3}, {2,4}, {3,4}}

What is a graph ? G=(V,E) V = {1,2,3,4,5} E = {{1,5}, {2,3}, {2,4}, {3,4}}

Connectedness connected not connected How can we check if a graph is connected?

Representing a graph |V| * |V| symmetric matrix A with A i,j = 1 if {i,j} E A i,j = 0 otherwise adjacency matrix

Representing a graph adjacency matrix space = (V 2 )

Representing a graph adjacency matrix space = (V 2 ) is {u,v} an edge ? (?) list neighbors of v (?)

Representing a graph adjacency matrix space = (V 2 ) is {u,v} an edge ? (1) list neighbors of v (n)

Representing a graph adjacency lists for each vertex v V linked list of neighbors of v

Representing a graph adjacency lists space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1

Representing a graph adjacency lists space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1 is {u,v} an edge ? (?) list neighbors of v (?)

Representing a graph adjacency lists space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1 is {u,v} an edge ? (min(d v,d u )) list neighbors of v (d v )

Representing a graph adjacency lists space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: space = (V 2 ) adjacency matrix is {u,v} in E ? (min{d u,d v }) neigbors of v ? (d v ) is {u,v} in E ? (1) neigbors of v ? (n)

Counting connected components How can we check if a graph is connected? INPUT: graph G given by adjacency list OUTPUT: number of components of G

BFS (G,v) seen[v] true enqueue(Q,v) while Q not empty do w dequeue(Q) for each neighbor u of w if not seen[u] then seen[u] true enqueue(Q,u) G – undirected graph, V={1,...,n} seen[v] = false for all v V Q=queue (FIFO)

Counting connected components C 0 for all v V do seen[v] false for all v V do if not seen[v] then C++ BFS(G,v) output G has C connected components

DFS explore(G,v) visited[v] true for each neighbor u of v if not visited(u) then explore(G,u) G – undirected graph, V={1,...,n} visited[v] = false for all v V

DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++ G – undirected graph, V={1,...,n} visited[v] = false for all v V

DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++ vertex I v := [pre[v],post[v]] interval property for u,v V either * I v and I u are disjoint, or * one is contained in the other

DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++ A B C D

DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++ A B C D tree edges

Digraphs (directed graphs) G=(V,E) V = a set of vertices E = a set of edges edge = ordered pair of vertices u v (u,v)

Digraphs (directed graphs) adjacency lists for each vertex v V linked list of out-neighbors of v |V| * |V| matrix A with A i,j = 1 if (i,j) E A i,j = 0 otherwise adjacency matrix

a path = sequence of vertices v 1,v 2,...,v k such that (v 1,v 2 ) E,..., (v k-1,v k ) E Digraphs (directed graphs)

DAGs (acyclic digraphs) a cycle = sequence of vertices v 1,v 2,...,v k such that (v 1,v 2 ) E,..., (v k-1,v k ),(v k,v 1 ) E DAG = digraph with no cycle

Topological sort (linearization) INPUT: DAG G given by adjacency list OUTPUT: ordering of vertices such that edges go forward

DFS on digraphs explore(G,v) visited[v] true pre[v] clock; clock++ for each out-neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++ G = digraph, V={1,...,n} visited[v] = false for all v V

DFS on digraphs A B C D

A B C D root descendant, ancestor child, parent

DFS on digraphs A B C D tree edge

DFS on digraphs A B C D tree edge

DFS on digraphs A B C D tree edge back edge

DFS on digraphs A B C D tree edge back edge cross edge

DFS on digraphs A B C D tree edge back edge cross edge forward edge

Relationships between the intervals? A B C D tree edge back edge cross edge forward edge

Topological sort using DFS Lemma: digraph is a DAG if and only if DFS has a back edge.

Topological sort using DFS Lemma: digraph is a DAG if and only if DFS has a back edge. explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++ Lemma: in a DAG every edge goes to a vertex with lower post

(strong) connectedness a digraph G is strongly connected if for every u,v V there exists a path from u to v in G

(strong) connectedness How to check if a digraph is strongly connected?

(strong) connectedness How to check if a digraph is strongly connected? for every u V do DFS(G,u) check if every v V was visited

(strong) connectedness How to check if a digraph is strongly connected? pick some u V DFS(G,u) check if every v V was visited DFS(reverse(G),u) check if every v V was visited

Strongly connected components DAG of strongly connected components

Strongly connected components Lemma: G and reverse(G) have the same strongly connected components.

Strongly connected components DAG of strongly connected components

Strongly connected components for all v V do color[v] white for all v V do if color[v]=white then DFS(reverse(G),v) DFS(G,u) (vertices in order post[])