CSCI 256 Data Structures and Algorithm Analysis Lecture 4 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

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.
Lectures on Graph Algorithms: searching, testing and sorting
Coloring Warm-Up. A graph is 2-colorable iff it has no odd length cycles 1: If G has an odd-length cycle then G is not 2- colorable Proof: Let v 0, …,
Connectivity - Menger’s Theorem Graphs & Algorithms Lecture 3.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4.
Graphs. Data structures that connect a set of objects to form a kind of a network Objects are called “Nodes” or “Vertices” Connections are called “Edges”
CSCI 256 Data Structures and Algorithm Analysis Lecture 18 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Computational Geometry Seminar Lecture 1
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
CSE 321 Discrete Structures Winter 2008 Lecture 25 Graph Theory.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
K-Coloring k-coloring: A k-coloring of a graph G is a labeling f: V(G)  S, where |S|=k. The labels are colors; the vertices of one color form a color.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
GRAPH Learning Outcomes Students should be able to:
Graphs CS /02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
 Jim has six children.  Chris fights with Bob,Faye, and Eve all the time; Eve fights (besides with Chris) with Al and Di all the time; and Al and Bob.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
1 Graphs - Definitions - Breadth First Search - Depth First Search - Connectivity - Topological Ordering.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 33.
Module #19: Graph Theory: part II Rosen 5 th ed., chs. 8-9.
3.1 Basic Definitions and Applications. Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise.
1 Chapter 8 NP and Computational Intractability Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Markov Chains and Random Walks. Def: A stochastic process X={X(t),t ∈ T} is a collection of random variables. If T is a countable set, say T={0,1,2, …
Data Structures & Algorithms Graphs
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
3.1 Basic Definitions and Applications
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
September1999 CMSC 203 / 0201 Fall 2002 Week #13 – 18/20/22 November 2002 Prof. Marie desJardins.
Graph Theory and Applications
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 25.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 6.
1 Chapter 3 Graphs Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs Lecture 2. Graphs (1) An undirected graph is a triple (V, E, Y), where V and E are finite sets and Y:E g{X V :| X |=2}. A directed graph or digraph.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSCI 256 Data Structures and Algorithm Analysis Lecture 20 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Introduction to Graph Theory
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.
Trees.
Operations Research II Course,, September Part 2: Network Flow Operations Research II Dr. Aref Rashad.
Graphs. Representations of graphs : undirected graph An undirected graph G have five vertices and seven edges An adjacency-list representation of G The.
Data Structures and Algorithm Analysis Lecture 5
3.1 Basic Definitions and Applications
Chapter 5. Optimal Matchings
Chapter 3 Graphs Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Planarity Testing.
CSE 421: Introduction to Algorithms
3.3 Applications of Maximum Flow and Minimum Cut
Richard Anderson Lecture 4
Lectures on Graph Algorithms: searching, testing and sorting
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
Searching in Graphs.
Graphs Definitions Breadth First Search Depth First Search
Richard Anderson Winter 2009 Lecture 6
3.2 Graph Traversal.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Algorithm Course Dr. Aref Rashad
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

CSCI 256 Data Structures and Algorithm Analysis Lecture 4 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

Graphs G = (V, E) –V = nodes, E = edges between pairs of nodes –Captures pairwise relationship between objects –Graph size parameters: n = |V|, m = |E| –Undirected graphs: edges are sets of two nodes {u, v} –Directed graphs: edges are ordered pairs (u, v) 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

Some Graph Applications transportation Graph street intersections NodesEdges highways communicationcomputersfiber optic cables World Wide Webweb pageshyperlinks socialpeoplerelationships food webspeciespredator-prey software systemsfunctionsfunction calls schedulingtasksprecedence constraints circuitsgateswires

Web Graph Node: web page Edge: hyperlink from one page to another cnn.com cnnsi.com novell.comnetscape.com timewarner.com hbo.com sorpranos.com

Paths and Connectivity Def: A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2, …, v k-1, v k with the property that each consecutive pair v i, v i+1 is joined by an edge in E. The distance between nodes u and v is min number of edges in a u- v path –Def: A path is simple if all nodes are distinct Def: An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v –Def: A directed graph is strongly connected if for every pair of nodes u and v, there is path from u to v and a path from v to u

Cycles Def: A cycle is a path v 1, v 2, …, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct Def for path, simple path and cycle carry over to directed graphs, with the following change: the sequence of nodes must respect the directionality of the edges cycle C = 1, 2, 4, 5, 3, 1

Trees Def: An undirected graph is a tree if it is connected and does not contain a cycle Indeed we have the following: Theorem: Let G be an undirected graph on n nodes. Any two of the following statements imply the third –G is connected –G does not contain a cycle –G has n-1 edges

Graph convention Generally if we say graph, we mean undirected graph incident adjacent neighbour

Rooted Trees Rooted tree: Given a tree T, choose a root node r and orient each edge away from r –Importance: Models hierarchical structure a tree the same tree, rooted at 1 v parent of v child of v root r

Phylogeny Trees Phylogeny trees: Describe evolutionary history of species

Graph Connectivity and Graph Traversal s-t connectivity problem: Given two node s and t, is there a path between s and t? s-t shortest path problem: Given two node s and t, what is the length of the shortest path between s and t?

Breadth First Search BFS intuition: Explore outward from s in all possible directions, adding nodes one "layer" at a time BFS algorithm –L 0 = { s } –L 1 = all neighbors of L 0 –L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge incident to a node in L 1 –L i+1 = all nodes that do not belong to an earlier layer, and that have an edge incident to a node in L i Theorem: For each i, L i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer s L1L1 L2L2 L n-1

Breadth First Search Property: Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G, and x is in L i and y is in L j. Then i and j differ by at most 1 (i.e., all edges go between nodes on the same layer or adjacent layers). Why? L0L0 L1L1 L2L2 L3L3

Breadth First Search Proof: By contradiction. Suppose i and j differ by more than 1, WLOG, suppose i < j-1; show you get a contradiction

Bipartite Graphs Def: An undirected graph G = (V, E) is bipartite if V can be partitioned into two sets V 1 and V 2 such that all edges go between V 1 and V 2 –A graph is bipartite if it can be two colored (e.g., the nodes can be colored red or blue such that every edge has one red and one blue end) a bipartite graph

Testing Bipartiteness Testing bipartiteness: Given a graph G, is it bipartite? –Many graph problems become: easier if the underlying graph is bipartite (matching) tractable if the underlying graph is bipartite (independent set) –Before attempting to design an algorithm, we need to understand structure of bipartite graphs v1v1 v2v2 v3v3 v6v6 v5v5 v4v4 v7v7 v2v2 v4v4 v5v5 v7v7 v1v1 v3v3 v6v6 a bipartite graph Ganother drawing of G

Testing Bipartiteness What are some examples of a nonbipartite graph? A triangle? A cycle of odd length?

An Obstruction to Bipartiteness Lemma A: If a graph G is bipartite, it cannot contain an odd length cycle –Pf: Not possible to 2-color the odd cycle, let alone G bipartite (2-colorable) not bipartite (not 2-colorable)

An Obstruction to Bipartiteness: Taking the contrapositive of the above lemma we may conclude: – if a graph G contains a cycle of odd length, then it is not bipartite

Testing Bipartiteness: Use BFS Lemma B: Let G be a connected graph, and let L 0, …, L k be the layers produced by BFS starting at node s. –(i) If no edge of G joins two nodes of the same layer, then G is bipartite –(ii) If an edge of G joins two nodes of the same layer, then G contains an odd-length cycle (and hence is not bipartite) Case (i) L1L1 L2L2 L3L3 Case (ii) L1L1 L2L2 L3L3

Testing Bipartiteness Pf: (i) –Suppose no edge joins two nodes in the same layer –By previous Property (slide 13), this implies all edges join nodes on adjacent layers; –the following coloring shows graph is bipartite: red = nodes on odd layers, blue = nodes on even layers Case (i) L1L1 L2L2 L3L3

Testing Bipartiteness Pf: (ii) –Suppose (x, y) is an edge with x, y in same layer L j –Let z = lca(x, y) = lowest common ancestor –Let L i be level containing z –Consider cycle that takes edge from x to y, then path from y to z, then path from z to x –Its length is 1 + (j-i) + (j-i), an odd number (x, y)path from y to z path from z to x z = lca(x, y)

An Obstruction to Bipartiteness The lemmas A and B allow us to conclude: A graph G is bipartite iff it contains no odd length cycle bipartite (2-colorable) not bipartite (not 2-colorable)

Proof: (iff ( ) proof generally requires two arguments) ( => ) Lemma A says: if bipartite then no odd length cycle ( <= ) contrapositive of Lemma B (ii) says: if no odd length cycle, then no edge joins nodes at same layer, so by (i) bipartite