Download presentation
Presentation is loading. Please wait.
1
Graph Theory TreesAlgorithms
2
Graphs: Basic Definitions 4 n 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
3
Trees 6 n A tree is a simple graph having the property that there is a unique path between every pair of nodes. n A rooted tree is one in which a particular node is designated as the root n 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 n A tree which is a subgraph of a graph G and contains every node of G is called a spanning tree of G n A node which is not a leaf is an internal node n Chartrand p. 87/ #1,2
4
Trees (cont.)
5
Spanning and Rooted trees n Petersen Graph (find spanning trees—Maple counttrees) n Rooted tree n Rooted tree 2
6
Theorem: Let T be a simple graph with n nodes. TFAE: n (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
7
Proof (partial) n (a) (b) n There is a path between every pair of nodes (definition of tree) n Hence, T is connected n If T had a cycle, then there would exist at least two paths from node a to node b n But the path from a to b is unique (by definition) n Hence, T is acyclic. nnnn n (b) (c),(c) (d)
8
Proof (partial--continued) n 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 T1,T2,…Tk are components (disjoint connected subgraphs) n Let Ti have ni nodes; no cycles, so each must have ni-1 edges n n-1=(n1-1)+(n2-1)+(n3-1)+…+(nk-1) < (n1+n2+n3+…+nk)-1=n-1 n Contradiction; T connected, so T is a tree
9
Uses of trees 9 n Ex/ Huffman codes n ASCII-each character encoded in a 7-bit string n A: 100 0000 n B:100 0001 n C: 100 0010 n 1: 011 0001 n 2:011 0010 n !: 010 0001 n *: 010 1010
10
Uses of trees (cont) n See board (BNF 15.3,15.4)
11
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
12
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.
13
DFS algorithm n STEP 1: (Start) Pick a node u and assign it label 1. Let k=1, ={u}, and = and X=u. n STEP 2:(check for completion) If contains each node of G, then Stop—G connected n 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 n (Back up) If X=u, then Stop. G is not connected. Else, replace X by the predecessor of X and go to Step 3
14
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 .
15
BFS algorithm n STEP 1: (Start) Pick a node u and assign it label 0. Let ={u}, and = and X=u. n 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. n 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.
16
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.
17
MWST algorithm--Kruskal 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. n STEP 2:(check for completion) If E contains n-1 edges, Stop; have MWST; else, go to Step 3. n 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.
18
Binary trees and traversals 6 n Binary Tree: a rooted tree in which each node has at most two children and each child is designated left child or right child. n Thus in a binary tree, each node may have 0,1, or 2 children n Left child—left and below parent n Right child—right and below parent n 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 n Right subtree—defined similarly
19
Binary trees and traversals 6 n 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.
20
Rooted Tree D B A C
21
Expression trees n 4.6. n Polish notation—Polish mathematician Lukasiewicz—no parens needed n RT4
22
Traversal 4 n Traversal: visit each node of a graph exactly once n BFS/DFS—traversal of a connected graph— nodes are “visited”, i.e., labeled, exactly once n A preorder traversal of a binary tree is characterized by visiting the parent before the children and the left child before the right child n Listing the nodes in the order they are visited is called a n Listing the nodes in the order they are visited is called a preorder listing
23
Preorder Traversal (i.e., DFS w/ choosing left before right) 4 n 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
24
Postorder Traversal 6 n RPN—Reverse Polish Notation n Operation sign is followed by the operands (HP calculator) n (2-3*4)+(4+8/2)=-2 n Pre: +-2*34+4/82 Preorder—look for operation sign followed by two numbers n 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
25
Postorder Traversal (child before parent, left before right) 4 n STEP 1: (Start) Go to the root n STEP 2:(go left) Go to the left subtree, if one exists, and do a postorder traversal n STEP 3: (go right) Go to the right subtree, if one exists, and do a postorder traversal. n Step 4: (Visit) Visit the root n 4.6.7, 4.6.8
26
Binary Search Tree n (to determine if an element a is in a binary search tree) n Step 1(Start) Let V be the root of the binary search tree. n Step 2 (compare) If a = V, then A is in the tree; STOP. Else, go to Step 3 n 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.