CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Similar documents
11/26/17. directed graphs. CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10. undirected graphs

Copyright 2000, Kevin Wayne 1

Undirected Graphs. 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

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graph Algorithms. Definition

Algorithm Design and Analysis

Graph Algorithms Using Depth First Search

Chapter 28 Graphs and Applications. Objectives

Graph Representation

Algorithm Design and Analysis

Algorithms: Lecture 10. Chalmers University of Technology

Elementary Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Outlines: Graphs Part-2

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

CS 310 Advanced Data Structures and Algorithms

Representations of Graphs

Graph: representation and traversal

W4231: Analysis of Algorithms

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

CSI 604 Elementary Graph Algorithms

Design and Analysis of Algorithms


Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

(Re)Introduction to Graphs and Some Algorithms

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

3.1 Basic Definitions and Applications

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Figure 1: A directed graph.

Announcements Problem Set 4 is out!

Trees. Arash Rafiey. 20 October, 2015

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Lecture 3: Graphs and flows

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Basic Graph Algorithms

Graph Representations and Traversal

Outline. Graphs. Divide and Conquer.

Lecture 9 Graph Traversal

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Goals! CSE 417: Algorithms and Computational Complexity!

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

TIE Graph algorithms

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

CMSC 380. Graph Terminology and Representation

Trees Algorhyme by Radia Perlman

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

CSC263 Week 8. Larry Zhang.

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Topic 12: Elementary Graph Algorithms

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

March 20/2003 Jayakanth Srinivasan,

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

Graph. Vertex. edge. Directed Graph. Undirected Graph

22.3 Depth First Search

22.1 Representations of graphs

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Review: Graph Theory and Representation

Graph representation

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS521 \ Notes for the Final Exam

CS/COE

Data Structures and Algorithms. Chapter 7. Graphs

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Graph Search Methods. Graph Search Methods

Graph Algorithms. Andreas Klappenecker

Chapter 22. Elementary Graph Algorithms

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Introductory Remarks

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS 206 Introduction to Computer Science II

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Undirected Graphs. Hwansoo Han

Basic Graph Algorithms (CLRS B.4-B.5, )

Breadth First Search. cse2011 section 13.3 of textbook

Konigsberg Bridge Problem

Searching in Graphs (cut points)

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

3.1 Basic Definitions and Applications

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

Graph Search. Adnan Aziz

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

DFS & STRONGLY CONNECTED COMPONENTS

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

TIE Graph algorithms

Transcription:

CS 220: Discrete Structures and their Applications graphs zybooks chapter 10

directed graphs A collection of vertices and directed edges What can this represent?

undirected graphs A collection of vertices and edges What can this represent?

Graph definitions Graph G = (V, E), V: set of nodes or vertices, E: set of edges (pairs of nodes). In an undirected graph, edges are unordered pairs (sets) of nodes. In a directed graph edges are ordered pairs of nodes. Path: sequence of nodes (v 0..v n ) s.t. "i: (v i,v i+1 ) is an edge. Path length: number of edges in the path, or sum of weights. Simple path: all nodes distinct. Cycle: path with first and last node equal. Acyclic graph: graph without cycles. DAG: directed acyclic graph. Two nodes are adjacent if there is an edge between them. In a complete graph all nodes in the graph are adjacent.

more definitions An undirected graph is connected if for all nodes v i and v j there is a path from v i to v j. An undirected graph can be partitioned in connected components: maximal connected sub-graphs. A directed graph can be partitioned in strongly connected components: maximal sub-graphs C where for every u and v in C there is a path from u to v and there is a path from v to u. G (V, E ) is a sub-graph of G(V,E) if V ÍV and E Í E The sub-graph of G induced by V has all the edges (u,v) Î E such that u Î V and v Î V. In a weighted graph the edges have a weight (cost, length,..) associated with them.

directed / undirected graphs in applications Directed or undirected graph? ü Facebook friend graph ü The "follow" graph ü The "like" graph ü Knowledge graph https://medium.com/basecs/a-gentle-introduction-to-graphtheory-77969829ead8 https://www.ambiverse.com/knowledge-graphsencyclopaedias-for-machines/

constraint graphs Consider the problem of classroom scheduling: given a set of classes and their times, assign them to classrooms without conflicts. Example: Class A: MWF, 3:00PM - 4:00PM Class B: W, 2:00PM - 4:00PM Class C: F, 3:30PM - 5:00PM Class D: MWF, 2:30-3:30PM Which is the constraint graph for this scheduling problem?

terminology G=(V, E) Vertices Edges Two vertices are adjacent if they are connected by an edge. Edges The vertices are the endpoints of an edge An edge is incident on two vertices. Vertices/ Nodes u e v Two vertices are neighbors if they are connected by an edge The number of neighbors of a vertex is its degree.

question What is the degree of c? A. 4 B. 5 C. 6 b c d a f e g

undirected graphs No self loops No "parallel" edges self loop: an edge that connects a vertex to itself simple graph: no self loops and no two edges that connect the same vertices. We will focus on simple graphs.

the handshake theorem Theorem: Let G=(V,E) be an undirected graph. Then deg(v) = 2 E v V

subgraphs A graph H = (V H, E H ) is a subgraph of a graph G = (V G, E G ) if V H V G and E H E G.

complete graphs A complete graph is a simple graph that has an edge between every pair of vertices. The complete graph with n vertices is denoted by K n K 4 : Complete Graph

cycles The cycle C n, n 3, consists of n vertices v 1, v 2,, v n and n edges {v 1, v 2 }, {v 2, v 3 },, {v n-1, v n }, {v n, v 1 }.

the n-dimensional hypercube The Hypercube Q 3 110 111 100 101 010 011 000 001

regular graphs A regular is a graph in which all vertices have the same degree. 110 111 100 010 101 011 000

looks can be misleading Consider the following two graphs: Are they the same?

adjacency matrix of a graph mapping of vertex labels to array indices B A C 0 1 2 3 4 0 0 1 0 1 0 Label Index 1 0 0 0 0 1 A 0 B 1 C 2 D 3 D E 2 1 0 0 0 0 3 0 1 0 0 0 4 0 0 1 0 0 E 4 For an undirected graph, what would the adjacency matrix look like? Adjacency matrix: n x n matrix with entries that indicate if an edge between two vertices is present

question Adjacency Matrix Is this the adjacency matrix of an undirected graph? A. Yes B. No 0 1 2 3 4 0 0 1 1 0 0 1 1 0 0 1 1 2 1 0 0 0 1 3 0 1 0 1 0 4 0 1 1 0 0

adjacency matrix Adjacency matrix for an undirected graph 1

question Adjacency Matrix Does this graph have self loops? A. Yes B. No 0 1 2 3 4 0 0 1 1 0 0 1 1 0 0 1 1 2 1 0 0 0 1 3 0 1 0 1 0 4 0 1 1 0 0

adjacency list for a directed graph A Index Label B C 0 A B D 1 B E 2 C A D 3 D B E 4 E C

adjacency list for an undirected graph A Index Label B C 0 A B C D 1 B A D E D E 2 C 3 D 4 E A A B E B C mapping of vertex labels to list of edges

Adjacency matrix which implementation Edges are entries in a square matrix size: V 2 values: 1/0 to indicate presence/absence of edge in (un)directed graph useful for dense graphs Adjacency list linked-list of out-going edges per vertex useful for sparse graphs

which implementation Which implementation best supports common graph operations: Is there an edge between vertex i and vertex j? Find all vertices adjacent to vertex j What's the big O for each of these operations? Which best uses space?

walks v 0 e 1 A walk from v 0 to v l in an undirected graph G is a sequence of alternating vertices and edges that starts and ends with a vertex: v 0,{v 0,v 1 },v 1,{v 1,v 2 },v 2,...,v l 1,{v l 1,v l },v l v 1 e 2 v 2 e 3 v 3 A walk can also be denoted by the sequence of vertices: v 0,v 1,...,v l. The sequence of vertices is a walk only if {v i-1, v i } E for i = 1, 2,...,l. The length of a walk is l, the number of edges in the walk.

walks, circuits, paths, cycles A circuit is a walk in which the first vertex is the same as the last vertex. A sequence of one vertex, denoted <a>, is a circuit of length 0. v 0 e 1 v 1 e 2 v 3 A walk is a path if no vertex is repeated in the walk. A circuit is a cycle if there are no other repeated vertices, except the first and the last. e 3 Same as in directed graphs. v 2

walks, circuits, paths, cycles A circuit is a walk in which the first vertex is the same as the last vertex. A walk is a path if no vertex is repeated in the walk. A circuit is a cycle if there are no other repeated vertices, except the first and the last. ² What is the length of the longest possible walk in a graph with n vertices? ² What is the length of the longest possible path in a graph with n vertices? ² What is the length of the longest possible circuit in a graph with n vertices? ² What is the length of the longest possible cycle in a graph with n vertices?

29 Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. How many edges does a tree have? Given a set of nodes, build a tree step wise every time you add an edge, you must add a new node to the growing tree. WHY? how many edges to connect n nodes?

30 Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge below r; do same for sub-trees. Models hierarchical structure. By rooting the tree it is easy to see that it has n-1 edges. root r parent of v v child of v a tree the same tree, rooted at 1

Traversing a Binary Tree Pre order visit the node go left go right In order go left visit the node go right B A Post order go left go right visit the node Level order / breadth first C for d = 0 to height visit nodes at level d D E F G H I

Traversal Examples Pre order A A B D G H C E F I B C In order G D H B A E C F I Post order D E F G H D B E I F C A G H I Level order A B C D E F G H I IMPLEMENTATION of these traversals??

Tree traversal Implementation recursive implementation of preorder The steps: visit node preorder(left child) preorder(right child) What changes need to be made for in-order, postorder? How would you implement level order?

Graph Traversal What makes it different from tree traversals?

Graph Traversal What makes it different from tree traversals: you can visit the same node more than once you can get in a cycle What to do about it?

Graph Traversal What makes it different from tree traversals: you can visit the same node more than once you can get in a cycle What to do about it: mark the nodes -White: unvisited -Grey: (still being considered) on the frontier: not all adjacent nodes have been visited yet -Black: off the frontier: all adjacent nodes visited (not considered anymore)

BFS: Breadth First Search Like level traversal in trees, BFS(G,s) explores the edges of G and locates every node v reachable from s in a level order using a queue.

BFS: Breadth First Search Like level traversal in trees, BFS(G,s) explores the edges of G and locates every node v reachable from s in a level order using a queue. BFS also computes the distance: number of edges from s to all these nodes, and the shortest path (minimal #edges) from s to v.

BFS: Breadth First Search Like level traversal in trees, BFS(G,s) explores the edges of G and locates every node v reachable from s in a level order using a queue. BFS also computes the distance: number of edges from s to all these nodes, and the shortest path (minimal #edges) from s to v. BFS expands a frontier of discovered but not yet visited nodes. Nodes are colored white, grey or black. They start out undiscovered or white.

40 Breadth First Search BFS intuition. Explore outward from s, adding nodes one "layer" at a time. BFS algorithm. L 0 = { s }. L 1 = all neighbors of L 0. s L 1 L 2 L n-1 L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. 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.

41 Breadth First Tree BFS produces a Breadth First (spanning) Tree rooted at s: when a node v in L i+1 is discovered as a neighbor of node u in L i we add edge (u,v) to the BF tree Property. Let T be a BFS tree of G, and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. WHY?

42 Breadth First Search L 0 L 1 L 2 L 3

BFS(G,s) #d: distance, c: color, p: parent in BFS tree forall v in V-s {c[v]=white; d[v]=,p[v]=nil} c[s]=grey; d[s]=0; p[s]=nil; Q=empty; enque(q,s); while (Q!= empty) u = deque(q); forall v in adj(u) if (c[v]==white) c[v]=grey; d[v]=d[u]+1; p[v]=u; enque(q,v) c[u]=black; # don t really need grey here, why?

Complexity BFS Each node is painted white once, and is enqueued and dequeued at most once.

Complexity BFS Each node is painted white once, and is enqueued and dequeued at most once. Why?

Complexity BFS Each node is painted white once, and is enqueued and dequeued at most once. Enque and deque take constant time. The adjacency list of each node is scanned only once: when it is dequeued.

Complexity BFS Each node is painted white once, and is enqueued and dequeued at most once. Enque and deque take constant time. The adjacency list of each node is scanned only once, when it is dequeued. Therefore time complexity for BFS is O( V + E ) or O(n+m)

DFS: Depth First Search Explores edges from the most recently discovered node; backtracks when reaching a dead-end. The algorithm below does not use white, grey, black, but uses explored (and implicitly unexplored). Recursive code: DFS(u): mark u as Explored and add u to R for each edge (u,v) : if v is not marked Explored : DFS(v) BUT, how do we find cycles in a graph?

49 DFS and cyclic graphs There are two ways DFS can revisit a node: 1. DFS has already fully explored the node. What color does it have then? Is there a cycle then? 2. DFS is still exploring this node. What color does it have in this case? Is there a cycle then?

50 DFS and cyclic graphs There are two ways DFS can revisit a node: 1. DFS has already fully explored the node. What color does it have then? Is there a cycle then? No, the node is revisited from outside. 2. DFS is still exploring this node. What color does it have in this case? Is there a cycle then? Yes, the node is revisited on a path containing the node itself. So DFS with the white, grey, black coloring scheme detects a cycle when a GREY node is visited.

51 Cycle detection: DFS + coloring When a grey (frontier) node is visited, a cycle is detected.

Recursive / node coloring version DFS(u): #c: color, p: parent c[u]=grey forall v in Adj(u): if c[v]==white: p[v]=u DFS(v) c[u]=black The above implementation of DFS runs in O(m + n) time if the graph is given by its adjacency list representation. Proof: Same as in BFS

53 DFS and cyclic graphs When DFS visits a node for the first time it is white. There are two ways DFS can revisit a node: 1. DFS has already fully explored the node. What color does it have then? Is there a cycle then? 2. DFS is still exploring this node. What color does it have in this case? Is there a cycle then?

54 Connectivity 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 nodes s and t, what is the length of the shortest path between s and t? Length: either in terms of number of edges, or in terms of sum of weights.

connected components An undirected graph is called connected if there is a path between every pair of vertices. A connected component is a maximal set of vertices that is connected. The word "maximal" means that if any vertex is added to a connected component, then the set of vertices will no longer be connected.

56 Connected Components Connected graph. There is a path between any pair of nodes. Connected component of a node s. The set of all nodes reachable from s. Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.

57 Connected Components Connected component of a node s. The set of all nodes reachable from s. Given two nodes s, and t, their connected components are either identical or disjoint. WHY?

58 Connected Components Connected component of a node s. The set of all nodes reachable from s. Given two nodes s, and t, their connected components are either identical or disjoint. Two cases either there is a path between the two nodes, or there isn t. If there is a path: take a node u in the connected component of s, and construct a path from t to u: t to s, then s to u, so CC s = CC t If there is no path: assume that the intersection contains a node u. Use it to construct a path between s and t: s to u, then u to t contradiction.

59 Connected Components A generic algorithm for finding connected components: s R u v R = {s} # the connected component of s is initially s. while there is an edge (u,v) where u is in R and v is not in R: add v to R Upon termination, R is the connected component containing s. BFS: explore in order of distance from s. DFS: explores edges from the most recently discovered node; backtracks when reaching a dead-end.

example How many connected components does this graph have? A. 0 B. 1 C. 2 e c a d f b

The facebook graph u u u 721 million active accounts 68.7 billion friendship edges (median number of friends = 99) The largest connected component of facebook users contains 99.9% of the users u Average distance between any pair of users: 4.7 source: http://arxiv.org/pdf/1111.4503v1.pdf