270 likes | 284 Views
Graph Theory. Trees Algorithms. Graphs: Basic Definitions 4. Let n be the number of nodes (stations) and e be the number of edges (links). A graph is connected if there is a sequence of nodes and edges between every pair of nodes.
E N D
Graph Theory Trees Algorithms
Graphs: Basic Definitions 4 • Let n be the number of nodes (stations) and e be the number of edges (links). • A graph is connected if there is a sequence of nodes and edges between every pair of nodes. • A cycle is a sequence of nodes and edges visiting none more than once, except for the first/last one • Degree: # of edges incident on node
Trees 6 • A tree is a simple graph having the property that there is a unique path between every pair of nodes. • A rootedtree 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 • A node which is not a leaf is an internal node • Chartrand p. 87/ #1,2
Spanning and Rooted trees • Petersen Graph (find spanning trees—Maple counttrees) • Rooted tree • Rooted tree 2
Theorem: Let T be a simple graph with n nodes. TFAE: • (a) T is a tree • (b) T is connected and acyclic • (c)T is connected and has n-1 edges • (d) T has n-1 edges and is acyclic
Proof (partial) • (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) • Assume acyclic and n-1 edges • RTS:tree(unique path betw. every node pair) • WTS:T connected • Assume not; then T1,T2,…Tk are components (disjoint connected subgraphs) • Let Ti have ni nodes; no cycles, so each must have ni-1 edges • n-1=(n1-1)+(n2-1)+(n3-1)+…+(nk-1) < (n1+n2+n3+…+nk)-1=n-1 • Contradiction; T connected, so T is a tree
Uses of trees 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) • See board (BNF 15.3,15.4)
Depth –first search • 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 • First strategy: depth-first search
DFS algorithm • 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 • 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 • 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 • 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) • This algorithm will find a minimal spanning tree, if one exists, for a weighted graph G with n nodes, e edges.
MWST algorithm--Kruskal • 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 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 • Node B has one child, left child D • Node C has right child E but no left child.
Rooted Tree D B A C
Expression trees • 4.6. • Polish notation—Polish mathematician Lukasiewicz—no parens needed • RT4
Traversal 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 • STEP 2:(go left) Go to the left subtree, if one exists, and do a preorder traversal • STEP 3: Go to the right subtree, if one exists, and do a preorder traversal. • 4.6.4,4.6.5
Postorder Traversal 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 • By using a traversal called postorder, can obtain the RPN for an expression
Postorder Traversal (child before parent, left before right) 4 • 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 • (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 • 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 • Step 4 (if larger, go right)