Graph Theory Trees Algorithms Graphs Basic Definitions 4
Graph Theory Trees Algorithms
Graphs: Basic Definitions 4 Let n be the number of nodes (stations) and e be the number of edges (links). n A graph is connected if there is a sequence of nodes and edges between every pair of nodes. n A cycle is a sequence of nodes and edges visiting none more than once, except for the first/last one n Degree: # of edges incident on node n
Trees n n A tree is a simple graph having the property that there is a unique path between every pair of nodes. A rooted tree is one in which a particular node is designated as the root A node in a rooted tree is a leaf if it is at root i (i>=0) and not adjacent to any nodes at level i+1 A tree which is a subgraph of a graph G and contains every node of G is called a spanning tree of G n n 6 A node which is not a leaf is an internal node Chartrand p. 87/ #1, 2
Trees (cont. )
Spanning and Rooted trees n Petersen Graph (find spanning trees—Maple counttrees) n Rooted n tree Rooted tree 2
Theorem: Let T be a simple graph with n nodes. TFAE: (a) T is a tree n (b) T is connected and acyclic n (c)T is connected and has n-1 edges n (d) T has n-1 edges and is acyclic n
Proof (partial) n n n n (a) (b) There is a path between every pair of nodes (definition of tree) Hence, T is connected If T had a cycle, then there would exist at least two paths from node a to node b But the path from a to b is unique (by definition) Hence, T is acyclic. (b) (c), (c) (d)
Proof (partial--continued) To show (d) (a) n Assume acyclic and n-1 edges n RTS: tree(unique path betw. every node pair) n WTS: T connected n Assume not; then T 1, T 2, …Tk are components (disjoint connected subgraphs) n Let Ti have ni nodes; no cycles, so each must have ni-1 edges n n-1=(n 1 -1)+(n 2 -1)+(n 3 -1)+…+(nk-1) < (n 1+n 2+n 3+…+nk)-1=n-1 n Contradiction; T connected, so T is a tree n
Uses of trees n n n n n 9 Ex/ Huffman codes ASCII-each character encoded in a 7 -bit string A: 100 0000 B: 100 0001 C: 100 0010 1: 011 0001 2: 011 0010 !: 010 0001 *: 010 1010
Uses of trees (cont) n See board (BNF 15. 3, 15. 4)
Depth –first search n Suppose we wish to search the nodes of a graph, beginning at a specified node. Two types of strategy could be used: – forge ahead, moving on to a new node whenever one is available – Spread out—checking all nodes at a each level before moving on n First strategy: depth-first search
DFS algorithm n This algorithm will assign labels to nodes and select edges of a graph G. is the set of nodes with labels, is the set of edges selected, and the predecessor of a node Y is a node in that is used in labeling Y.
DFS algorithm n n STEP 1: (Start) Pick a node u and assign it label 1. Let k=1, ={u}, and = and X=u. STEP 2: (check for completion) If contains each node of G, then Stop—G connected STEP 3: (Find next node and edge) Find node Y not in that is adjacent to X; if no such node, go to Step 4. Otherwise, put{X, Y} in , increment k to k+1, assign the label k to Y, and put Y in . Replace X by Y, and go to Step 2 (Back up) If X=u, then Stop. G is not connected. Else, replace X by the predecessor of X and go to Step 3
BFS algorithm n This algorithm will find a spanning tree, if it exists, for a graph on n nodes. In the algorithm, is the set of nodes with labels and is the set of edges connecting nodes in .
BFS algorithm n n n STEP 1: (Start) Pick a node u and assign it label 0. Let ={u}, and = and X=u. STEP 2: (check for completion) If | |<n, go to Step 3. Otherwise, if | |=n, stop; the edges in and the nodes in form a spanning tree for G. STEP 3: (label the nodes) Find the nodes not in that are adjacent to nodes in having the largest label number; call it k. If there are no such nodes, then the graph has no spanning tree. Otherwise, assign the newly found nodes the label k+1, and put them in . For each new node with label k+1, place in one edge joining this node to a node with label k. If there is more than one such edge, choose one arbitrarily. Go to Step 2.
Minimum Weight Spanning Tree algorithm (Kruskal) n This algorithm will find a minimal spanning tree, if one exists, for a weighted graph G with n nodes, e edges.
MWST algorithm--Kruskal n n n STEP 1: (Start) If no edges, G is not connected; else, pick an edge with the smallest weight (ties can be broken arbitrarily). Place edge in E and node in N. STEP 2: (check for completion) If E contains n 1 edges, Stop; have MWST; else, go to Step 3. STEP 3: (pick next edge) Find the edges of smallest weight which do not form a cycle with any of the edges of E. If no such edges, G is not connected and there is no spanning tree. Else, choose one such edge and place it in E and the nodes in N. Go to Step 2.
Binary trees and traversals n n n 6 Binary Tree: a rooted tree in which each node has at most two children and each child is designated left child or right child. Thus in a binary tree, each node may have 0, 1, or 2 children Left child—left and below parent Right child—right and below parent The left subtree of a node N in a binary tree is the graph formed by the left child L, the descendants of L, and the edges connecting these nodes Right subtree—defined similarly
Binary trees and traversals 6 A is the root, A has two children, left child B and right child C n Node B has one child, left child D n Node C has right child E but no left child. n
Rooted Tree B A C D
Expression trees 4. 6. n Polish notation—Polish mathematician Lukasiewicz—no parens needed n RT 4 n
Traversal n n 4 Traversal: visit each node of a graph exactly once BFS/DFS—traversal of a connected graph— nodes are “visited” , i. e. , labeled, exactly once A preorder traversal of a binary tree is characterized by visiting the parent before the children and the left child before the right child Listing the nodes in the order they are visited is called a preorder listing
Preorder Traversal (i. e. , DFS w/ choosing left before right) 4 STEP 1: (Visit) Visit the root n STEP 2: (go left) Go to the left subtree, if one exists, and do a preorder traversal n STEP 3: Go to the right subtree, if one exists, and do a preorder traversal. n 4. 6. 4, 4. 6. 5 n
Postorder Traversal n n n 6 RPN—Reverse Polish Notation Operation sign is followed by the operands (HP calculator) (2 -3*4)+(4+8/2)=-2 Pre: +-2*34+4/82 Preorder—look for operation sign followed by two numbers Post: 234*-482/++ – Postorder—look for two condecutive numbers followed by an operation n By using a traversal called postorder, can obtain the RPN for an expression
Postorder Traversal (child before parent, left before right) 4 n n n STEP 1: (Start) Go to the root STEP 2: (go left) Go to the left subtree, if one exists, and do a postorder traversal STEP 3: (go right) Go to the right subtree, if one exists, and do a postorder traversal. Step 4: (Visit) Visit the root 4. 6. 7, 4. 6. 8
Binary Search Tree n n (to determine if an element a is in a binary search tree) Step 1(Start) Let V be the root of the binary search tree. Step 2 (compare) If a = V, then A is in the tree; STOP. Else, go to Step 3 (if smaller, go left) If V<=a, go to Step 4. Otherwise, a<=V – If no left child of V, a is not in tree. STOP – Else, V has left child L; let V=L and go to Step 2 n Step 4 (if larger, go right)
- Slides: 26