Text Book: Introduction to algorithms By C L R S

Slides:



Advertisements
Similar presentations
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Advertisements

More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Chapter 23 Minimum Spanning Trees
CS138A Network Flows Peter Schröder. CS138A Flow Networks Definitions a flow network G=(V,E) is a directed graph in which each edge (u,v)
1 Data Structures DFS, Topological Sort Dana Shapira.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
IS 2610: Data Structures Graph April 5, 2004.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
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 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.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Instructor Neelima Gupta Edited by Divya Gaur(39, MCS '09) Thanks to: Bhavya(9), Deepika(10), Deepika Bisht(11) (MCS '09)
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
CS138A Elementary Graph Algorithms Peter Schröder.
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
Introduction to Algorithms
Elementary Graph Algorithms
Maximum Flow c v 3/3 4/6 1/1 4/7 t s 3/3 w 1/9 3/5 1/1 3/5 u z 2/2
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.
Maximum Flow Chapter 26.
CSC317 Shortest path algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Graph Algorithms Minimum Spanning Tree (Chap 23)
CS4234 Optimiz(s)ation Algorithms
Topological Sort Minimum Spanning Tree
Network flow problem [Adapted from M.Chandy].
CSC 413/513: Intro to Algorithms
Lecture 12 Algorithm Analysis
Introduction to Graphs
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
ADVANCED ALGORITHMS GRAPH ALGORITHMS (UNIT-2).
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Greedy Technique.
Maximum Flow c v 3/3 4/6 1/1 4/7 t s 3/3 w 1/9 3/5 1/1 3/5 u z 2/2
CS6045: Advanced Algorithms
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Graph Representation (23.1/22.1)
CS 583 Analysis of Algorithms
Analysis of Algorithms CS 477/677
Flow Networks Topics Flow Networks Residual networks
Chapter 11 Graphs.
Lecture 12 Algorithm Analysis
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Flow Networks General Characteristics Applications
Flow Networks and Bipartite Matching
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms (2IL15) – Lecture 7
Algorithms Searching in a Graph.
Maximum Flow c v 3/3 4/6 1/1 4/7 t s 3/3 w 1/9 3/5 1/1 3/5 u z 2/2
Lecture 12 Algorithm Analysis
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
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 .
Minimum Spanning Trees
Presentation transcript:

Text Book: Introduction to algorithms By C L R S Graph Algorithms Text Book: Introduction to algorithms By C L R S

Graphs - Definitions A directed graph (or digraph) G is a pair (V,E), where V is a finite set and E is a binary relation on V . The set V is called the vertex set of G, and its elements are called vertices The set E is called the edge set of G, and its elements are called edges. Vertices are usually represented by circles and edges are represented by arrows.

Graphs - Definitions In an undirected graph G = (V,E), the edge set E consists of unordered pairs of vertices, rather than ordered pairs. If (u,v) is an edge in a directed graph G = (V,E), we say that (u,v) is incident from or leaves vertex u and is incident to or enters vertex v. If (u,v) is an edge in an undirected graph G =(V,E), we say that (u,v) is incident on vertices u and v.

Graphs - Definitions If (u,v) is an edge in a graph G =(V,E), we say that vertex v is adjacent to vertex u. When the graph is undirected, the adjacency relation is symmetric. The degree of a vertex in an undirected graph is the number of edges incident on it. A vertex whose degree is 0 is isolated.

Graphs - Definitions In a directed graph, the out-degree of a vertex is the number of edges leaving it, and the in-degree of a vertex is the number of edges entering it. The degree of a vertex in a directed graph is its in-degree plus its out-degree.

Graphs - Definitions A path of length k from a vertex u to a vertex u’ in a graph G=(V,E) is a sequence <v0, v1, . . . ,vk> of vertices such that u=v0, u’=vk, and (vi-1,vi) є E for i= 1, 2, 3, . . . , k. An undirected graph is connected if every vertex is reachable from all other vertices. The connected components of a graph are the equivalence classes of vertices under the “is reachable from” relation.

Graphs - Definitions An undirected graph is connected if it has exactly one connected component. A directed graph is strongly connected if every two vertices are reachable from each other.

Representation of graphs

Representation of graphs

Representation of graphs

Representation of graphs

BFS

BFS

BFS Example

BFS Example

BFS Example

Properties of BFS Running time O(V + E) BFS computes shorted path distance Algorithm BFS builds a breadth first tree.

Printing shortest path

DFS

DFS

DFS Example

DFS Example

DFS Example

DFS Example

DFS Example

DFS – Example 2

DFS – Example 2

parenthesis theorem In any depth-first search of a (directed undirected) graph G=(V,E), for any two vertices u and v, exactly one of the following three conditions holds: the intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and neither u nor v is a descendant of the other in the depth-first forest, the interval [u.d, u.f] is contained entirely within the interval [v.d, v.f], and u is a descendant of v in a depth-first tree, or the interval [v.d, v.f] is contained entirely within the interval [u.d, u.f], and v is a descendant of u in a depth-first tree.

Classification of edges Tree edges are edges in the depth-first forest Gpi. Edge (u,v) is a tree edge if v was first discovered by exploring edge (u,v) Back edges are those edges (u,v) connecting a vertex u to an ancestor v in a depth-first tree. We consider self-loops, which may occur in directed graphs, to be back edges. Forward edges are those nontree edges (u,v) connecting a vertex u to a descendant v in a depth-first tree. Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees.

Classification of edges when we first explore an edge (u,v), the color of vertex v WHITE indicates a tree edge, GRAY indicates a back edge, and BLACK indicates a forward or cross edge.

Topological sort

Topological sort - algorithm

Topological sort

Strongly connected components Strongly connected component of a directed graph G = (V,E) is a maximal set of vertices C where C is a subset of V such that for every pair of vertices u and v in C, vertices u and v are reachable from each other.

Strongly connected components- Algorithm STRONGLY-CONNECTED-COMPONENTS(G) 1 call DFS(G) to compute finishing times u.f for each vertex u 2 compute GT 3 call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing u.f (as computed in line 1) 4 output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component

Strongly connected components- Example

Strongly connected components- Example

Component Graph

Minimal Spanning tree

Minimal Spanning tree

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Example

Kruskal’s Algorithm Running time O(E lg V)

Prim’s Algorithm

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Prim’s Algorithm - Example

Generic MST Algorithm

Light Edge A cut (S, V – S) of an undirected graph G=(V,E) is a partition of V . An edge (u,v) є E crosses the cut (S, V-S) if one of its endpoints is in S and the other is in V - S. A cut respects a set A of edges if no edge in A crosses the cut. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

Safe Edge - Theorem Let G=(V,E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S, V-S). Then, edge (u,v) is safe for A.

Cut - Example

Cut – Another View

Shortest Path The weight w(p) of path p = <v0, v1, . . . , vk> is the sum of the weights of its constituent edges:

Shortest Path Problems single-source shortest-paths problem Single-destination shortest-paths problem Single-pair shortest-path problem All-pairs shortest-paths problem

Shortest Path Problems Property Subpaths of shortest paths are shortest paths Graphs with negative edges Graphs with cycles Can a shortest path contain a cycle?

Are shortest paths unique?

Are shortest paths unique?

Are shortest paths unique?

Shortest Path Algorithms v.d is a shortest path estimate to v from s

Relaxing The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to v found so far by going through u and, if so, updating v.d and v.π.

Relaxing

Properties of shortest paths and relaxation

Properties of shortest paths and relaxation

Bellman Ford Algorithm

Bellman Ford Example

Bellman Ford Example

Bellman Ford Example

Bellman Ford Example

Bellman Ford Example

shortest path in DAG

Dijkstra’s algorithm

Dijkstra’s algorithm Can be used only when all weights are non-negative It maintains a set S of vertices whose final shortest-path weights from the source s have already been determined. Algorithm uses a greedy strategy. It chooses the “lightest” or “closest” vertex in V - S

Dijkstra’s algorithm - Example

Dijkstra’s algorithm - Example

Dijkstra’s algorithm - Example

Dijkstra’s algorithm - Example

Dijkstra’s algorithm - Example

Dijkstra’s algorithm - Example

The Floyd-Warshall algorithm The Floyd-Warshall algorithm considers the intermediate vertices of a shortest path, where an intermediate vertex of a simple path p = <v1,v2,…vl> is any vertex of p other than v1 or vl

The Floyd-Warshall algorithm

The Floyd-Warshall algorithm

Printing Shortest Path

Filling predecessor Matrix

Transitive closure of a graph We define the transitive closure of G as the graph G* = (V,E*), where E* = { (i,j): there is a path from vertex i to vertex j in G} One way to compute the transitive closure of a graph is to assign a weight of 1 to each edge of E and run the Floyd-Warshall algorithm.

Transitive closure Algorithm

Transitive closure Algorithm

Flow Networks G = (V,E) directed. Each edge (u,v) has a capacity c(u,v) >= 0 If (u,v) not є E, then c(u,v) = 0. If (u,v) є E, then reverse edge (v,u) not є E. Source vertex s, sink vertex t , assume s  v  t for all v є V , so that each vertex lies on a path from source to sink.

Flow Network

Another Flow Network

Flow A function f: V x V -> R satisfying Capacity constraint For all u,v є V, 0 <= f(u,v) <= c(u,v) Flow Constraint

Flows

Another Flow

Multiple sources

Maximum Flow Problem The value |f| of a flow f is defined as In the maximum-flow problem, we are given a flow network G with source s and sink t , and we wish to find a flow of maximum value.

Network with anti parallel edges

Converted graph

The Ford-Fulkerson method FORD-FULKERSON-METHOD (G, s, t) 1 initialize flow f to 0 2 while there exists an augmenting path p in the residual network Gf 3 augment flow f along p 4 return f

Residual network

Residual network Given a flow network G = (V,E) and a flow f , the residual network of G induced by f is Gf =(V, Ef), where

Residual network

Augmenting flow If f is a flow in G and f’ is a flow in the corresponding residual network Gf , we define f ↑ f’, the augmentation of flow f by f’, to be a function from V x V to R, defined by

Augmenting paths An augmenting path p is a simple path from s to t in the residual network Gf Residual Capacity of path p The maximum amount by which we can increase the flow on each edge in an augmenting path p cf(p) = min { cf(u,v): (u,v) is on p}

Updated Flow Network

Cuts of flow networks A cut (S,T) of flow network G = (V,E) is a partition of V into S and T = V - S such that s є S and t є T . If f is a flow, then the net flow f(S,T) across the cut (S,T) is defined to be Capacity of cut (S,T) is

Example of a cut

Flows through a cut Lemma: Let f be a flow in a flow network G with source s and sink t, and let (S,T) be any cut of G. Then the net flow across (S,T) is f(S,T) = |f| Corollary The value of any flow f in a flow network G is bounded from above by the capacity of any cut of G.

Max-flow Min-cut Theorem If f is a flow in a flow network G = (V,E) with source s and sink t , then the following conditions are equivalent: 1. f is a maximum flow in G. 2. The residual network Gf contains no augmenting paths. 3|f| = c(S,T) for some cut (S,T) of G.

Ford Fulkerson Algorithm

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Example

Ford Fulkerson - Complexity O( E. |f|)

Maximum bipartite matching Given a graph G=(V,E) A matching is a subset of edges M of E such that for all vertices v in V, at most one edge of M is incident on v. A maximum matching is a matching of maximum cardinality

Maximum bipartite matching

Maximum bipartite matching

Maximum bipartite matching