Richard Anderson Autumn 2016 Lecture 5

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101A - Design and Analysis of Algorithms 10
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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.
Leda Demos By: Kelley Louie Credits: definitions from Algorithms Lectures and Discrete Mathematics with Algorithms by Albertson and Hutchinson graphics.
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
Data Structures and Algorithm Analysis Lecture 5
Breadth-First Search (BFS)
Graphs A New Data Structure
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,
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Lecture 12 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,
Depth-First Search.
Directed Graphs 5/1/15 12:25:22 PM
CS120 Graphs.
Graph Algorithms Using Depth First Search
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Graph Search Applications
Planarity Testing.
CSE 421: Introduction to Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Richard Anderson Lecture 4
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Elementary Graph Algorithms
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
CSE 421: Introduction to Algorithms
Directed Graph Algorithms
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Richard Anderson Autumn 2016 Lecture 7
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
CSE 373: Data Structures and Algorithms
Lecture 11 CSE 331 Sep 19, 2014.
Text Book: Introduction to algorithms By C L R S
Lecture 6 Graph Traversal
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
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 .
Presentation transcript:

Richard Anderson Autumn 2016 Lecture 5 CSE 421 Algorithms Richard Anderson Autumn 2016 Lecture 5

Announcements Reading Chapter 3 (Mostly review) Start on Chapter 4

Graph Theory G = (V, E) Undirected graphs Directed graphs Explain that there will be some review from 326 Graph Theory G = (V, E) V – vertices E – edges Undirected graphs Edges sets of two vertices {u, v} Directed graphs Edges ordered pairs (u, v) Many other flavors Edge / vertices weights Parallel edges Self loops By default |V| = n and |E| = m

Definitions Neighborhood Path: v1, v2, …, vk, with (vi, vi+1) in E Simple Path Cycle Simple Cycle Neighborhood N(v) Distance Connectivity Undirected Directed (strong connectivity) Trees Rooted Unrooted

Graph search Find a path from s to t S = {s} while S is not empty u = Select(S) visit u foreach v in N(u) if v is unvisited Add(S, v) Pred[v] = u if (v = t) then path found

Breadth first search Explore vertices in layers s in layer 1 Neighbors of s in layer 2 Neighbors of layer 2 in layer 3 . . . s

Key observation All edges go between vertices on the same layer or adjacent layers 1 2 3 4 5 6 7 8

Bipartite Graphs A graph V is bipartite if V can be partitioned into V1, V2 such that all edges go between V1 and V2 A graph is bipartite if it can be two colored

Can this graph be two colored?

Algorithm Run BFS Color odd layers red, even layers blue If no edges between the same layer, the graph is bipartite If edge between two vertices of the same layer, then there is an odd cycle, and the graph is not bipartite

Theorem: A graph is bipartite if and only if it has no odd cycles If a graph has an odd cycle it is not bipartite If a graph does not have an odd cycle, it is bipartite

Lemma 1 If a graph contains an odd cycle, it is not bipartite

Lemma 2 If a BFS tree has an intra-level edge, then the graph has an odd length cycle Intra-level edge: both end points are in the same level

Lemma 3 If a graph has no odd length cycles, then it is bipartite No odd length cycles implies no intra-level edges No intra-level edges implies two colorability

Graph Search Data structure for next vertex to visit determines search order

Graph search Breadth First Search S = {s} while S is not empty u = Dequeue(S) if u is unvisited visit u foreach v in N(u) Enqueue(S, v) Depth First Search S = {s} while S is not empty u = Pop(S) if u is unvisited visit u foreach v in N(u) Push(S, v)

Breadth First Search All edges go between vertices on the same layer or adjacent layers 1 2 3 4 5 6 7 8

Depth First Search Each edge goes between vertices on the same branch 1 Each edge goes between vertices on the same branch No cross edges 2 6 3 4 7 12 5 8 9 10 11

Connected Components Undirected Graphs

Computing Connected Components in O(n+m) time A search algorithm from a vertex v can find all vertices in v’s component While there is an unvisited vertex v, search from v to find a new component

Directed Graphs A Strongly Connected Component is a subset of the vertices with paths between every pair of vertices.

Identify the Strongly Connected Components There is an O(n+m) algorithm that we will not be covering

Strongly connected components can be found in O(n+m) time But it’s tricky! Simpler problem: given a vertex v, compute the vertices in v’s scc in O(n+m) time

Topological Sort Given a set of tasks with precedence constraints, find a linear order of the tasks 311 312 431 142 143 341 332 421 351 451 333

Find a topological order for the following graph J C F K B L

If a graph has a cycle, there is no topological sort Consider the first vertex on the cycle in the topological sort It must have an incoming edge A F B E C D

Lemma: If a graph is acyclic, it has a vertex with in degree 0 Proof: Pick a vertex v1, if it has in-degree 0 then done If not, let (v2, v1) be an edge, if v2 has in-degree 0 then done If not, let (v3, v2) be an edge . . . If this process continues for more than n steps, we have a repeated vertex, so we have a cycle

Topological Sort Algorithm While there exists a vertex v with in-degree 0 Output vertex v Delete the vertex v and all out going edges H E I A D G J C F K B L

Details for O(n+m) implementation Maintain a list of vertices of in-degree 0 Each vertex keeps track of its in-degree Update in-degrees and list when edges are removed m edge removals at O(1) cost each