Presentation is loading. Please wait.

Presentation is loading. Please wait.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 1/18 Module.

Similar presentations


Presentation on theme: "Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 1/18 Module."— Presentation transcript:

1

2 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 1/18 Module #22 - Graphs Bogazici University Department of Computer Engineering CmpE 220 Discrete Mathematics 22. Graph Theory Haluk Bingöl

3 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 2/18 Module #22 - Graphs Module #22: Graph Theory Rosen 5 th ed., chs. 8-9 ~44 slides (more later), ~3 lectures

4 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 3/18 Module #22 - Graphs What are Graphs? General meaning in everyday math: A plot or chart of numerical data using a coordinate system. Technical meaning in discrete mathematics: A particular class of discrete structures (to be defined) that is useful for representing relations and has a convenient webby- looking graphical representation. Not

5 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 4/18 Module #22 - Graphs Applications of Graphs Potentially anything (graphs can represent relations, relations can describe the extension of any predicate). Apps in networking, scheduling, flow optimization, circuit design, path planning. More apps: Geneology analysis, computer game-playing, program compilation, object-oriented design, …

6 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 5/18 Module #22 - Graphs Simple Graphs Correspond to symmetric, irreflexive binary relations R. Def. A simple graph G=(V,E) consists of: a set V of vertices or nodes (V corresponds to the universe of the relation R), a set E of edges / arcs / links: unordered pairs of [distinct] elements u,v  V, such that uRv. Visual Representation of a Simple Graph

7 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 6/18 Module #22 - Graphs Let V be the set of states in the far- southeastern U.S.: I.e., V={FL, GA, AL, MS, LA, SC, TN, NC} Let E={{u,v}|u adjoins v} ={{FL,GA},{FL,AL},{FL,MS}, {FL,LA},{GA,AL},{AL,MS}, {MS,LA},{GA,SC},{GA,TN}, {SC,NC},{NC,TN},{MS,TN}, {MS,AL}} Example of a Simple Graph TN AL MS LA SC GA FL NC

8 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 7/18 Module #22 - Graphs Multigraphs Like simple graphs, but there may be more than one edge connecting two given nodes. A multigraph G=(V, E, f ) consists of a set V of vertices, a set E of edges (as primitive objects), and a function f:E  {{u,v}|u,v  V  u  v}. E.g., nodes are cities, edges are segments of major highways. Parallel edges

9 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 8/18 Module #22 - Graphs Pseudographs Like a multigraph, but edges connecting a node to itself are allowed. (R may be reflexive.) A pseudograph G=(V, E, f ) where f:E  {{u,v}|u,v  V}. Edge e  E is a loop if f(e)={u,u}={u}. E.g., nodes are campsites in a state park, edges are hiking trails through the woods.

10 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 9/18 Module #22 - Graphs Directed Graphs Correspond to arbitrary binary relations R, which need not be symmetric. A directed graph (V,E) consists of a set of vertices V and a binary relation E on V. E.g.: V = set of People, E={(x,y) | x loves y}

11 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 10/18 Module #22 - Graphs Directed Multigraphs Like directed graphs, but there may be more than one arc from a node to another. A directed multigraph G=(V, E, f ) consists of a set V of vertices, a set E of edges, and a function f:E  V  V. E.g., V=web pages, E=hyperlinks. The WWW is a directed multigraph...

12 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 11/18 Module #22 - Graphs Types of Graphs: Summary Summary of the book’s definitions. Keep in mind this terminology is not fully standardized across different authors...

13 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 12/18 Module #22 - Graphs §8.2: Graph Terminology You need to learn the following terms: Adjacent, connects, endpoints, degree, initial, terminal, in-degree, out-degree, complete, cycles, wheels, n-cubes, bipartite, subgraph, union.

14 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 13/18 Module #22 - Graphs Adjacency Let G be an undirected graph with edge set E. Let e  E be (or map to) the pair {u,v}. Then we say: u, v are adjacent / neighbors / connected. Edge e is incident with vertices u and v. Edge e connects u and v. Vertices u and v are endpoints of edge e.

15 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 14/18 Module #22 - Graphs Degree of a Vertex Let G be an undirected graph, v  V a vertex. The degree of v, deg(v), is its number of incident edges. (Except that any self-loops are counted twice.) A vertex with degree 0 is called isolated. A vertex of degree 1 is called pendant.

16 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 15/18 Module #22 - Graphs Handshaking Theorem Let G be an undirected (simple, multi-, or pseudo-) graph with vertex set V and edge set E. Then Corollary: Any undirected graph has an even number of vertices of odd degree.

17 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 16/18 Module #22 - Graphs Directed Adjacency Let G be a directed (possibly multi-) graph, and let e be an edge of G that is (or maps to) (u,v). Then we say: u is adjacent to v, v is adjacent from u e comes from u, e goes to v. e connects u to v, e goes from u to v the initial vertex of e is u the terminal vertex of e is v

18 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 17/18 Module #22 - Graphs Directed Degree Let G be a directed graph, v a vertex of G. The in-degree of v, deg  (v), is the number of edges going to v. The out-degree of v, deg  (v), is the number of edges coming from v. The degree of v, deg(v):  deg  (v)+deg  (v), is the sum of v’s in-degree and out-degree.

19 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 18/18 Module #22 - Graphs Directed Handshaking Theorem Let G be a directed (possibly multi-) graph with vertex set V and edge set E. Then: Note that the degree of a node is unchanged by whether we consider its edges to be directed or undirected.

20 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 19/18 Module #22 - Graphs Special Graph Structures Special cases of undirected graph structures: Complete graphs K n Cycles C n Wheels W n n-Cubes Q n Bipartite graphs Complete bipartite graphs K m,n

21 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 20/18 Module #22 - Graphs Complete Graphs For any n  N, a complete graph on n vertices, K n, is a simple graph with n nodes in which every node is adjacent to every other node:  u,v  V: u  v  {u,v}  E. K1K1 K2K2 K3K3 K4K4 K5K5 K6K6 Note that K n has edges.

22 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 21/18 Module #22 - Graphs Cycles For any n  3, a cycle on n vertices, C n, is a simple graph where V={v 1,v 2,…,v n } and E={{v 1,v 2 },{v 2,v 3 },…,{v n  1,v n },{v n,v 1 }}. C3C3 C4C4 C5C5 C6C6 C7C7 C8C8 How many edges are there in C n ?

23 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 22/18 Module #22 - Graphs Wheels For any n  3, a wheel W n, is a simple graph obtained by taking the cycle C n and adding one extra vertex v hub and n extra edges {{v hub,v 1 }, {v hub,v 2 },…,{v hub,v n }}. W3W3 W4W4 W5W5 W6W6 W7W7 W8W8 How many edges are there in W n ?

24 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 23/18 Module #22 - Graphs n-cubes (hypercubes) For any n  N, the hypercube Q n is a simple graph consisting of two copies of Q n-1 connected together at corresponding nodes. Q 0 has 1 node. Q0Q0 Q1Q1 Q2Q2 Q3Q3 Q4Q4 Number of vertices: 2 n. Number of edges:Exercise to try!

25 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 24/18 Module #22 - Graphs n-cubes (hypercubes) For any n  N, the hypercube Q n can be defined recursively as follows: Q 0 ={{v 0 },  } (one node and no edges) Q 0 ={{v 0 },  } (one node and no edges) For any n  N, if Q n =(V,E), where V={v 1,…,v a } and E={e 1,…,e b }, then Q n+1 =(V  {v 1 ´,…,v a ´}, E  {e 1 ´,…,e b ´}  {{v 1,v 1 ´},{v 2,v 2 ´},…, {v a,v a ´}}) where v 1 ´,…,v a ´ are new vertices, and where if e i ={v j,v k } then e i ´={v j ´,v k ´}.

26 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 25/18 Module #22 - Graphs Def’n.: A graph G=(V,E) is bipartite (two- part) iff V = V 1 ∩ V 2 where V 1  V 2 =  and  e  E:  v 1  V 1,v 2  V 2 : e={v 1,v 2 }. In English: The graph can be divided into two parts in such a way that all edges go between the two parts. Bipartite Graphs V1V1 V2V2 This definition can easily be adapted for the case of multigraphs and directed graphs as well. Can represent with zero-one matrices.

27 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 26/18 Module #22 - Graphs Complete Bipartite Graphs For m,n  N, the complete bipartite graph K m,n is a bipartite graph where |V 1 | = m, |V 2 | = n, and E = {{v 1,v 2 }|v 1  V 1  v 2  V 2 }. That is, there are m nodes in the left part, n nodes in the right part, and every node in the left part is connected to every node in the right part. K 4,3 K m,n has _____ nodes and _____ edges.

28 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 27/18 Module #22 - Graphs Subgraphs A subgraph of a graph G=(V,E) is a graph H=(W,F) where W  V and F  E. G H

29 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 28/18 Module #22 - Graphs Graph Unions The union G 1  G 2 of two simple graphs G 1 =(V 1, E 1 ) and G 2 =(V 2,E 2 ) is the simple graph (V 1  V 2, E 1  E 2 ). a bc d e a bc d f 

30 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 29/18 Module #22 - Graphs §8.3: Graph Representations & Isomorphism Graph representations: Adjacency lists. Adjacency matrices. Incidence matrices. Graph isomorphism: Two graphs are isomorphic iff they are identical except for their node names.

31 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 30/18 Module #22 - Graphs Adjacency Lists A table with 1 row per vertex, listing its adjacent vertices. a b d c f e

32 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 31/18 Module #22 - Graphs Directed Adjacency Lists 1 row per node, listing the terminal nodes of each edge incident from that node.

33 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 32/18 Module #22 - Graphs Adjacency Matrices A way to represent simple graphs possibly with self-loops. Matrix A=[a ij ], where a ij is 1 if {v i, v j } is an edge of G, and is 0 otherwise. Can extend to pseudographs by letting each matrix elements be the number of links (possibly >1) between the nodes.

34 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 33/18 Module #22 - Graphs Graph Isomorphism Formal definition: Simple graphs G 1 =(V 1, E 1 ) and G 2 =(V 2, E 2 ) are isomorphic iff  a bijection f:V 1  V 2 such that  a,b  V 1, a and b are adjacent in G 1 iff f(a) and f(b) are adjacent in G 2. f is the “renaming” function between the two node sets that makes the two graphs identical. This definition can easily be extended to other types of graphs.

35 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 34/18 Module #22 - Graphs Graph Invariants under Isomorphism Necessary but not sufficient conditions for G 1 =(V 1, E 1 ) to be isomorphic to G 2 =(V 2, E 2 ): We must have that |V1|=|V2|, and |E1|=|E2|. The number of vertices with degree n is the same in both graphs. For every proper subgraph g of one graph, there is a proper subgraph of the other graph that is isomorphic to g.

36 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 35/18 Module #22 - Graphs Isomorphism Example If isomorphic, label the 2nd graph to show the isomorphism, else identify difference. a b c d e f b d a e f c

37 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 36/18 Module #22 - Graphs Are These Isomorphic? If isomorphic, label the 2nd graph to show the isomorphism, else identify difference. a b c d e Same # of vertices Same # of edges Different # of verts of degree 2! (1 vs 3)

38 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 37/18 Module #22 - Graphs §8.4: Connectivity In an undirected graph, a path of length n from u to v is a sequence of adjacent edges going from vertex u to vertex v. A path is a circuit if u=v. A path traverses the vertices along it. A path is simple if it contains no edge more than once.

39 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 38/18 Module #22 - Graphs Paths in Directed Graphs Same as in undirected graphs, but the path must go in the direction of the arrows.

40 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 39/18 Module #22 - Graphs Connectedness An undirected graph is connected iff there is a path between every pair of distinct vertices in the graph. Theorem: There is a simple path between any pair of vertices in a connected undirected graph. Connected component: connected subgraph A cut vertex or cut edge separates 1 connected component into 2 if removed.

41 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 40/18 Module #22 - Graphs Directed Connectedness A directed graph is strongly connected iff there is a directed path from a to b for any two verts a and b. It is weakly connected iff the underlying undirected graph (i.e., with edge directions removed) is connected. Note strongly implies weakly but not vice- versa.

42 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 41/18 Module #22 - Graphs Paths & Isomorphism Note that connectedness, and the existence of a circuit or simple circuit of length k are graph invariants with respect to isomorphism.

43 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 42/18 Module #22 - Graphs Counting Paths w Adjacency Matrices Let A be the adjacency matrix of graph G. The number of paths of length k from v i to v j is equal to (A k ) i,j. The notation (M) i,j denotes m i,j where [m i,j ] = M.

44 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 43/18 Module #22 - Graphs §8.5: Euler & Hamilton Paths An Euler circuit in a graph G is a simple circuit containing every edge of G. An Euler path in G is a simple path containing every edge of G. A Hamilton circuit is a circuit that traverses each vertex in G exactly once. A Hamilton path is a path that traverses each vertex in G exactly once.

45 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 44/18 Module #22 - Graphs Bridges of Königsberg Problem Can we walk through town, crossing each bridge exactly once, and return to start? A B C D The original problem Equivalent multigraph

46 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 45/18 Module #22 - Graphs Euler Path Theorems Theorem: A connected multigraph has an Euler circuit iff each vertex has even degree. Proof: (→) The circuit contributes 2 to degree of each node. ( ← ) By construction using algorithm on p. 580-581 Theorem: A connected multigraph has an Euler path (but not an Euler circuit) iff it has exactly 2 vertices of odd degree. One is the start, the other is the end.

47 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 46/18 Module #22 - Graphs Euler Circuit Algorithm Begin with any arbitrary node. Construct a simple path from it till you get back to start. Repeat for each remaining subgraph, splicing results back into original cycle.

48 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 47/18 Module #22 - Graphs Round-the-World Puzzle Can we traverse all the vertices of a dodecahedron, visiting each once?` Dodecahedron puzzle Equivalent graph Pegboard version

49 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 48/18 Module #22 - Graphs Hamiltonian Path Theorems Dirac’s theorem: If (but not only if) G is connected, simple, has n  3 vertices, and  v deg(v)  n/2, then G has a Hamilton circuit. Ore’s corollary: If G is connected, simple, has n≥3 nodes, and deg(u)+deg(v)≥n for every pair u,v of non-adjacent nodes, then G has a Hamilton circuit.

50 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 49/18 Module #22 - Graphs HAM-CIRCUIT is NP-complete Let HAM-CIRCUIT be the problem: Given a simple graph G, does G contain a Hamiltonian circuit? This problem has been proven to be NP-complete! This means, if an algorithm for solving it in polynomial time were found, it could be used to solve all NP problems in polynomial time.

51 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 50/18 Module #22 - Graphs §8.6: Shortest-Path Problems Not covering this semester.

52 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 51/18 Module #22 - Graphs §8.7: Planar Graphs Not covering this semester.

53 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 52/18 Module #22 - Graphs §8.8: Graph Coloring Not covering this semester.

54 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 53/18 Module #22 - Graphs §9.1: Introduction to Trees A tree is a connected undirected graph that contains no circuits. Theorem: There is a unique simple path between any two of its nodes. A (not-necessarily-connected) undirected graph without simple circuits is called a forest. You can think of it as a set of trees having disjoint sets of nodes. A leaf node in a tree or forest is any pendant or isolated vertex. An internal node is any non- leaf vertex (thus it has degree ≥ ___ ).

55 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 54/18 Module #22 - Graphs Tree and Forest Examples A Tree: A Forest: Leaves in green, internal nodes in brown.

56 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 55/18 Module #22 - Graphs Rooted Trees A rooted tree is a tree in which one node has been designated the root. Every edge is (implicitly or explicitly) directed away from the root. You should know the following terms about rooted trees: Parent, child, siblings, ancestors, descendents, leaf, internal node, subtree.

57 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 56/18 Module #22 - Graphs Rooted Tree Examples Note that a given unrooted tree with n nodes yields n different rooted trees. root Same tree except for choice of root

58 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 57/18 Module #22 - Graphs Rooted-Tree Terminology Exercise Find the parent, children, siblings, ancestors, & descendants of node f. a b c d e f g h i jk l m n o p q r root

59 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 58/18 Module #22 - Graphs n-ary trees A rooted tree is called n-ary if every vertex has no more than n children. It is called full if every internal (non-leaf) vertex has exactly n children. A 2-ary tree is called a binary tree. These are handy for describing sequences of yes-no decisions. Example: Comparisons in binary search algorithm.

60 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 59/18 Module #22 - Graphs Which Tree is Binary? Theorem: A given rooted tree is a binary tree iff every node other than the root has degree ≤ ___, and the root has degree ≤ ___.

61 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 60/18 Module #22 - Graphs Ordered Rooted Tree This is just a rooted tree in which the children of each internal node are ordered. In ordered binary trees, we can define: left child, right child left subtree, right subtree For n-ary trees with n>2, can use terms like “leftmost”, “rightmost,” etc.

62 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 61/18 Module #22 - Graphs Trees as Models Can use trees to model the following: Saturated hydrocarbons Organizational structures Computer file systems In each case, would you use a rooted or a non-rooted tree?

63 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 62/18 Module #22 - Graphs Some Tree Theorems Any tree with n nodes has e = n−1 edges. Proof: Consider removing leaves. A full m-ary tree with i internal nodes has n=mi+1 nodes, and =(m−1)i+1 leaves. Proof: There are mi children of internal nodes, plus the root. And, = n−i = (m−1)i+1. □ Thus, when m is known and the tree is full, we can compute all four of the values e, i, n, and, given any one of them.

64 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 63/18 Module #22 - Graphs Some More Tree Theorems Definition: The level of a node is the length of the simple path from the root to the node. The height of a tree is maximum node level. A rooted m-ary tree with height h is called balanced if all leaves are at levels h or h−1. Theorem: There are at most m h leaves in an m- ary tree of height h. Corollary: An m-ary tree with leaves has height h≥  log m . If m is full and balanced then h=  log m .

65 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 64/18 Module #22 - Graphs §9.2: Applications of Trees Binary search trees A simple data structure for sorted lists Decision trees Minimum comparisons in sorting algorithms Prefix codes Huffman coding Game trees

66 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 65/18 Module #22 - Graphs Binary Search Trees A representation for sorted sets of items. Supports the following operations in Θ(log n) average-case time: Searching for an existing item. Inserting a new item, if not already present. Supports printing out all items in Θ(n) time. Note that inserting into a plain sequence a i would instead take Θ(n) worst-case time.

67 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 66/18 Module #22 - Graphs Binary Search Tree Format Items are stored at individual tree nodes. We arrange for the tree to always obey this invariant: For every item x, Every node in x’s left subtree is less than x. Every node in x’s right subtree is greater than x. 7 312 15915 02811 Example:

68 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 67/18 Module #22 - Graphs Recursive Binary Tree Insert procedure insert(T: binary tree, x: item) v := root[T] if v = null then begin root[T] := x; return “Done” end else if v = x return “Already present” else if x v} return insert(rightSubtree[T], x)

69 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 68/18 Module #22 - Graphs Decision Trees (pp. 646-649) A decision tree represents a decision-making process. Each possible “decision point” or situation is represented by a node. Each possible choice that could be made at that decision point is represented by an edge to a child node. In the extended decision trees used in decision analysis, we also include nodes that represent random events and their outcomes.

70 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 69/18 Module #22 - Graphs Coin-Weighing Problem Imagine you have 8 coins, one of which is a lighter counterfeit, and a free-beam balance. No scale of weight markings is required for this problem! How many weighings are needed to guarantee that the counterfeit coin will be found? ?

71 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 70/18 Module #22 - Graphs As a Decision-Tree Problem In each situation, we pick two disjoint and equal-size subsets of coins to put on the scale. The balance then “decides” whether to tip left, tip right, or stay balanced. A given sequence of weighings thus yields a decision tree with branching factor 3.

72 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 71/18 Module #22 - Graphs Applying the Tree Height Theorem The decision tree must have at least 8 leaf nodes, since there are 8 possible outcomes. In terms of which coin is the counterfeit one. Recall the tree-height theorem, h≥  log m . Thus the decision tree must have height h ≥  log 3 8  =  1.893…  = 2. Let’s see if we solve the problem with only 2 weighings…

73 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 72/18 Module #22 - Graphs General Solution Strategy The problem is an example of searching for 1 unique particular item, from among a list of n otherwise identical items. Somewhat analogous to the adage of “searching for a needle in haystack.” Armed with our balance, we can attack the problem using a divide-and-conquer strategy, like what’s done in binary search. We want to narrow down the set of possible locations where the desired item (coin) could be found down from n to just 1, in a logarithmic fashion. Each weighing has 3 possible outcomes. Thus, we should use it to partition the search space into 3 pieces that are as close to equal-sized as possible. This strategy will lead to the minimum possible worst-case number of weighings required.

74 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 73/18 Module #22 - Graphs General Balance Strategy On each step, put  n/3  of the n coins to be searched on each side of the scale. If the scale tips to the left, then: The lightweight fake is in the right set of  n/3  ≈ n/3 coins. If the scale tips to the right, then: The lightweight fake is in the left set of  n/3  ≈ n/3 coins. If the scale stays balanced, then: The fake is in the remaining set of n − 2  n/3  ≈ n/3 coins that were not weighed! Except if n mod 3 = 1 then we can do a little better by weighing  n/3  of the coins on each side. You can prove that this strategy always leads to a balanced 3-ary tree.

75 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 74/18 Module #22 - Graphs Coin Balancing Decision Tree Here’s what the tree looks like in our case: 123 vs 456 1 vs. 2 left: 123 balanced: 78 right: 456 7 vs. 8 4 vs. 5 L:1 R:2B:3 L:4 R:5B:6 L:7 R:8

76 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 75/18 Module #22 - Graphs Prefix Codes & Huffman Coding pp. 649-651

77 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 76/18 Module #22 - Graphs Game Trees pp. 651-656

78 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 77/18 Module #22 - Graphs §9.3: Tree Traversal Universal address systems Traversal algorithms Depth-first traversal: Preorder traversal Inorder traversal Postorder traversal Breadth-first traversal Infix/prefix/postfix notation

79 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 78/18 Module #22 - Graphs §9.4: Spanning Trees Not covering this semester.

80 Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 79/18 Module #22 - Graphs §9.5: Minimum Spanning Trees Not covering this semester.


Download ppt "Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 1/18 Module."

Similar presentations


Ads by Google