






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to graph theory, focusing on applications, connected components, and topological sorting. It covers various graph applications such as transportation, communication, and social networks. The document also explains the concepts of connected components, cycles, and bipartite graphs. Additionally, it discusses the topological sorting algorithm and its importance in determining the order of tasks in a directed acyclic graph (dag).
Typology: Slides
1 / 12
This page cannot be seen from the preview
Don't miss anything!







1
3
Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = |V|, m = |E|. 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 4
transportation Graph street intersections Nodes Edges highways communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey software systems functions function calls scheduling tasks precedence constraints circuits gates wires
5
Web graph. Node: web page. Edge: hyperlink from one page to another. cnn.com netscape.com novell.com cnnsi.com timewarner.com hbo.com sorpranos.com 6
Social network graph. Node: people. Edge: relationship between two people. Reference: Valdis Krebs, http://www.firstmonday.org/issues/issue7_4/krebs 7
Food web graph. Node = species. Edge = from prey to predator. Reference: http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff 8
Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. Two representations of each edge. Space proportional to n^2. Checking if (u, v) is an edge takes Θ(1) time. Identifying all edges takes Θ(n^2 ) time. 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0
13
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 14
Phylogeny trees. Describe evolutionary history of species. 15
Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html GUI containment hierarchy. Describe organization of GUI widgets.
17
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? Applications. Friendster. Maze traversal. Kevin Bacon number. Fewest number of hops in a communication network. 18
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 to a node in L 1. Li+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in Li. Theorem. For each i, Li 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 L 1 L 2 L (^) n- 19
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. L 0 L 1 L 2 L 3 20
Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency representation. Pf. Easy to prove O(n^2 ) running time:
26
Def. An undirected graph G = (V, E) is bipartite if the nodes can be colored red or blue such that every edge has one red and one blue end. Applications. Stable marriage: men = red, women = blue. Scheduling: machines = red, jobs = blue. a bipartite graph 27
Testing bipartiteness. Given a graph G, is it bipartite? Many graph problems become:
Lemma. 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)
29
Lemma. Let G be a connected graph, and let L 0 , …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Case (i) L 1 L 2 L 3 Case (ii) L 1 L 2 L 3 30
Lemma. Let G be a connected graph, and let L 0 , …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Pf. (i) Suppose no edge joins two nodes in adjacent layers. By previous lemma, this implies all edges join nodes on same level. Bipartition: red = nodes on odd levels, blue = nodes on even levels. Case (i) L 1 L 2 L 3 31
Lemma. Let G be a connected graph, and let L 0 , …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Pf. (ii) Suppose (x, y) is an edge with x, y in same level Lj. Let z = lca(x, y) = lowest common ancestor. Let Li 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), which is odd. ▪ z = lca(x, y) (x, y) path from y to z path from z to x 32
Corollary. A graph G is bipartite iff it contain no odd length cycle. 5-cycle C bipartite (2-colorable) not bipartite (not 2-colorable)
37
Theorem. Can determine if G is strongly connected in O(m + n) time. Pf. Pick any node s. Run BFS from s in G. Run BFS from s in Grev. Return true iff all nodes reached in both BFS executions. Correctness follows immediately from previous lemma. ▪ reverse orientation of every edge in G strongly connected not strongly connected
39
Def. An DAG is a directed graph that contains no directed cycles. Ex. Precedence constraints: edge (vi, vj) means vi must precede vj. Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v 1 , v 2 , …, vn so that for every edge (vi, vj) we have i < j. a DAG a topological ordering v 2 v 3 v 6 v 5 v 4 v 7 v 1 v 1 v 2 v 3 v 4 v 5 v 6 v 7 40
Precedence constraints. Edge (vi, vj) means task vi must occur before vj. Applications. Course prerequisite graph: course vi must be taken before vj. Compilation: module vi must be compiled before vj. Pipeline of computing jobs: output of job vi needed to determine input of job vj.
41
Lemma. If G has a topological order, then G is a DAG. Pf. (by contradiction) Suppose that G has a topological order v 1 , …, vn and that G also has a directed cycle C. Let's see what happens. Let vi be the lowest-indexed node in C, and let vj be the node just before vi; thus (vj, vi) is an edge. By our choice of i, we have i < j. On the other hand, since (vj, vi) is an edge and v 1 , …, vn is a topological order, we must have j < i, a contradiction. ▪ v 1 vi vj vn the supposed topological order: v 1 , …, vn the directed cycle C 42
Lemma. If G has a topological order, then G is a DAG. Q. Does every DAG have a topological ordering? Q. If so, how do we compute one? 43
Lemma. If G is a DAG, then G has a node with no incoming edges. Pf. (by contradiction) Suppose that G is a DAG and every node has at least one incoming edge. Let's see what happens. Pick any node v, and begin following edges backward from v. Since v has at least one incoming edge (u, v) we can walk backward to u. Then, since u has at least one incoming edge (x, u), we can walk backward to x. Repeat until we visit a node, say w, twice. Let C denote the sequence of nodes encountered between successive visits to w. C is a cycle. ▪ w x u v 44
Lemma. If G is a DAG, then G has a topological ordering. Pf. (by induction on n) Base case: true if n = 1. Given DAG on n > 1 nodes, find a node v with no incoming edges. G - { v } is a DAG, since deleting v cannot create cycles. By inductive hypothesis, G - { v } has a topological ordering. Place v first in topological ordering; then append nodes of G - { v } in topological order. This is valid since v has no incoming edges. ▪ DAG v