graph theory treesalgorithms. graphs: basic definitions 4 n let n be the number of nodes (stations)...

26
Graph Theory Graph Theory Trees Trees Algorithms Algorithms

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph Theory TreesAlgorithms. 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

Graph TheoryGraph Theory

TreesTrees

AlgorithmsAlgorithms

Page 2: Graph Theory TreesAlgorithms. 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

Graphs: Basic Definitions Graphs: Basic Definitions 44

Let n be the number of nodes (stations) Let n be the number of nodes (stations) and e be the number of edges (links).and e be the number of edges (links).

A graph is A graph is connectedconnected if there is a if there is a sequence of nodes and edges between sequence of nodes and edges between every pair of nodes.every pair of nodes.

A A cyclecycle is a sequence of nodes and edges is a sequence of nodes and edges visiting none more than once, except for visiting none more than once, except for the first/last onethe first/last one

Degree: # of edges incident on nodeDegree: # of edges incident on node

Page 3: Graph Theory TreesAlgorithms. 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

Trees Trees 6 6

A A tree tree is a simple graph having the property is a simple graph having the property that there is a unique path between every that there is a unique path between every pair of nodes.pair of nodes.

A A rootedrooted tree tree is one in which a particular is one in which a particular node is designated as the rootnode is designated as the root

A node in a rooted tree is a A node in a rooted tree is a leaf leaf if it is at root if it is at root i i (i>=0) and not adjacent to any nodes at (i>=0) and not adjacent to any nodes at level level i+1i+1

A tree which is a subgraph of a graph G and A tree which is a subgraph of a graph G and contains every node of G is called a contains every node of G is called a spanning tree of Gspanning tree of G

A node which is not a leaf is an A node which is not a leaf is an internal nodeinternal node Chartrand p. 87/ #1,2Chartrand p. 87/ #1,2

Page 4: Graph Theory TreesAlgorithms. 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

Trees (cont.)Trees (cont.)  

 

Page 5: Graph Theory TreesAlgorithms. 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

Spanning and Rooted Spanning and Rooted treestrees

Petersen Graph (find spanning trees—Maple Petersen Graph (find spanning trees—Maple counttrees)counttrees)

Rooted tree Rooted tree Rooted tree 2Rooted tree 2

 

Page 6: Graph Theory TreesAlgorithms. 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

Theorem: Let T be a Theorem: Let T be a simple graph with n simple graph with n

nodes. TFAE:nodes. TFAE:

(a) T is a (a) T is a treetree (b) T (b) T is connected and acyclicis connected and acyclic (c)T is connected and has (c)T is connected and has n-1 n-1

edgesedges (d) T has (d) T has n-1 n-1 edges and is acyclicedges and is acyclic

Page 7: Graph Theory TreesAlgorithms. 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

Proof (partial)Proof (partial) (a)(a)(b)(b) There is a path between every pair of nodes There is a path between every pair of nodes

(definition of tree)(definition of tree) Hence, T is connectedHence, T is connected If T had a cycle, then there would exist at least If T had a cycle, then there would exist at least

two paths from node a to node btwo paths from node a to node b But the path from a to b is unique (by But the path from a to b is unique (by

definition)definition) Hence, T is acyclic.Hence, T is acyclic. (b)(b)(c),(c)(c),(c)(d)(d)

Page 8: Graph Theory TreesAlgorithms. 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

Proof (partial--continued)Proof (partial--continued) To show (d)To show (d)(a)(a) Assume acyclic and n-1 edgesAssume acyclic and n-1 edges RTS:tree(unique path betw. every node pair)RTS:tree(unique path betw. every node pair) WTS:T connectedWTS:T connected Assume not; then T1,T2,…Tk are componentsAssume not; then T1,T2,…Tk are components(disjoint connected subgraphs) (disjoint connected subgraphs) Let Ti have ni nodes; no cycles, so each must Let Ti have ni nodes; no cycles, so each must

have ni-1 edgeshave ni-1 edges n-1=(n1-1)+(n2-1)+(n3-1)+…+(nk-1) < n-1=(n1-1)+(n2-1)+(n3-1)+…+(nk-1) <

(n1+n2+n3+…+nk)-1=n-1(n1+n2+n3+…+nk)-1=n-1 Contradiction; T connected, so T is a treeContradiction; T connected, so T is a tree

Page 9: Graph Theory TreesAlgorithms. 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

Uses of trees Uses of trees 99

Ex/ Huffman codesEx/ Huffman codes ASCII-each character encoded in a 7-bit ASCII-each character encoded in a 7-bit

stringstring A: 100 0000A: 100 0000 B:100 0001B:100 0001 C: 100 0010C: 100 0010 1: 011 00011: 011 0001 2:011 00102:011 0010 !: 010 0001!: 010 0001 *: 010 1010*: 010 1010

Page 10: Graph Theory TreesAlgorithms. 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

Uses of trees (cont)Uses of trees (cont)

See board (BNF 15.3,15.4)See board (BNF 15.3,15.4)

Page 11: Graph Theory TreesAlgorithms. 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

Depth –first searchDepth –first search

Suppose we wish to search the Suppose we wish to search the nodes of a graph, beginning at a nodes of a graph, beginning at a specified node. Two types of specified node. Two types of strategy could be used: strategy could be used: – forge ahead, moving on to a new forge ahead, moving on to a new

node whenever one is availablenode whenever one is available– Spread out—checking all nodes at a Spread out—checking all nodes at a

each level before moving oneach level before moving on First strategy: depth-first searchFirst strategy: depth-first search

Page 12: Graph Theory TreesAlgorithms. 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

DFS algorithmDFS algorithm This algorithm will assign labels to This algorithm will assign labels to

nodes and select edges of a graph nodes and select edges of a graph G. G. is the set of nodes with labels, is the set of nodes with labels, is the set of edges selected, and is the set of edges selected, and the predecessor of a node Y is a the predecessor of a node Y is a node in node in that is used in labeling Y. that is used in labeling Y.

Page 13: Graph Theory TreesAlgorithms. 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

DFS algorithmDFS algorithm STEP 1: (Start) Pick a node u and assign it STEP 1: (Start) Pick a node u and assign it

label 1. Let k=1, label 1. Let k=1, ={u}, and ={u}, and = = and X=u. and X=u. STEP 2:(check for completion) If STEP 2:(check for completion) If contains contains

each node of G, then Stop—G connectedeach node of G, then Stop—G connected STEP 3:(Find next node and edge) Find node STEP 3:(Find next node and edge) Find node

Y not in Y not in that is adjacent to X; if no such that is adjacent to X; if no such node, go to Step 4. Otherwise, put{X,Y} in node, go to Step 4. Otherwise, put{X,Y} in , , increment k to k+1, assign the label k to Y, increment k to k+1, assign the label k to Y, and put Y in and put Y in . Replace X by Y, and go to . Replace X by Y, and go to Step 2Step 2

(Back up) If X=u, then Stop. G is not (Back up) If X=u, then Stop. G is not connected. Else, replace X by the connected. Else, replace X by the predecessor of X and go to Step 3 predecessor of X and go to Step 3

Page 14: Graph Theory TreesAlgorithms. 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

BFS algorithmBFS algorithm This algorithm will find a spanning This algorithm will find a spanning

tree, if it exists, for a graph on n tree, if it exists, for a graph on n nodes. In the algorithm, nodes. In the algorithm, is the is the set of nodes with labels and set of nodes with labels and is is the set of edges connecting nodes the set of edges connecting nodes in in ..

Page 15: Graph Theory TreesAlgorithms. 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

BFS algorithmBFS algorithm STEP 1: (Start) Pick a node u and assign it STEP 1: (Start) Pick a node u and assign it

label 0. Let label 0. Let ={u}, and ={u}, and = = and X=u. and X=u. STEP 2:(check for completion) If |STEP 2:(check for completion) If ||<n, go to |<n, go to

Step 3. Otherwise, if |Step 3. Otherwise, if ||=n, stop; the edges in |=n, stop; the edges in and the nodes in and the nodes in form a spanning tree for form a spanning tree for G.G.

STEP 3: (label the nodes) Find the nodes not STEP 3: (label the nodes) Find the nodes not in in that are adjacent to nodes in that are adjacent to nodes in having having the largest label number; call it k. If there are the largest label number; call it k. If there are no such nodes, then the graph has no no such nodes, then the graph has no spanning tree. Otherwise, assign the newly spanning tree. Otherwise, assign the newly found nodes the label k+1, and put them in found nodes the label k+1, and put them in . For each new node with label k+1, place in . For each new node with label k+1, place in one edge joining this node to a node with one edge joining this node to a node with label k. If there is more than one such edge, label k. If there is more than one such edge, choose one arbitrarily. Go to Step 2.choose one arbitrarily. Go to Step 2.

Page 16: Graph Theory TreesAlgorithms. 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

Minimum Weight Spanning Minimum Weight Spanning Tree algorithm (Kruskal)Tree algorithm (Kruskal) This algorithm will find a minimal This algorithm will find a minimal

spanning tree, if one exists, for a spanning tree, if one exists, for a weighted graph G with n nodes, e weighted graph G with n nodes, e edges.edges.

Page 17: Graph Theory TreesAlgorithms. 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

MWST algorithm--KruskalMWST algorithm--Kruskal STEP 1: (Start) If no edges, G is not STEP 1: (Start) If no edges, G is not

connected; else, pick an edge with the connected; else, pick an edge with the smallest weight (ties can be broken smallest weight (ties can be broken arbitrarily). Place edge in E and node in N.arbitrarily). Place edge in E and node in N.

STEP 2:(check for completion) If E STEP 2:(check for completion) If E contains n-1 edges, Stop; have MWST; contains n-1 edges, Stop; have MWST; else, go to Step 3.else, go to Step 3.

STEP 3: (pick next edge) Find the edges STEP 3: (pick next edge) Find the edges of smallest weight which do not form a of smallest weight which do not form a cycle with any of the edges of E. If no cycle with any of the edges of E. If no such edges, G is not connected and there such edges, G is not connected and there is no spanning tree. Else, choose one is no spanning tree. Else, choose one such edge and place it in E and the nodes such edge and place it in E and the nodes in N. Go to Step 2.in N. Go to Step 2.

Page 18: Graph Theory TreesAlgorithms. 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

Binary trees and Binary trees and traversals traversals 66

Binary TreeBinary Tree: a rooted tree in which each : a rooted tree in which each node has at most two children and each node has at most two children and each child is designated child is designated left childleft child or or right childright child..

Thus in a binary tree, each node may have Thus in a binary tree, each node may have 0,1, or 2 children0,1, or 2 children

Left child—left and below parentLeft child—left and below parent Right child—right and below parentRight child—right and below parent The The left subtreeleft subtree of a node N in a binary of a node N in a binary

tree is the graph formed by the left child tree is the graph formed by the left child L, the descendants of L, and the edges L, the descendants of L, and the edges connecting these nodesconnecting these nodes

Right subtree—defined similarlyRight subtree—defined similarly

Page 19: Graph Theory TreesAlgorithms. 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

Binary trees and Binary trees and traversals traversals 66

A is the root, A has two children, A is the root, A has two children, left child B and right child Cleft child B and right child C

Node B has one child, left child DNode B has one child, left child D Node C has right child E but no left Node C has right child E but no left

child.child.

Page 20: Graph Theory TreesAlgorithms. 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

Rooted TreeRooted Tree

   D

B A C

Page 21: Graph Theory TreesAlgorithms. 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

Expression treesExpression trees 4.6.4.6. Polish notation—Polish Polish notation—Polish

mathematician Lukasiewicz—no mathematician Lukasiewicz—no parens neededparens needed

RT4RT4

Page 22: Graph Theory TreesAlgorithms. 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

Traversal Traversal 44

Traversal: visit each node of a graph Traversal: visit each node of a graph exactly onceexactly once

BFS/DFS—traversal of a connected graph—BFS/DFS—traversal of a connected graph—nodes are “visited” , i.e., labeled, exactly nodes are “visited” , i.e., labeled, exactly onceonce

A A preorder traversalpreorder traversal of a binary tree is of a binary tree is characterized by visiting the parent before characterized by visiting the parent before the children and the left child before the the children and the left child before the right childright child

Listing the nodes in the order they are Listing the nodes in the order they are visited is called a visited is called a preorder listing

Page 23: Graph Theory TreesAlgorithms. 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

Preorder Traversal (i.e., Preorder Traversal (i.e., DFS w/ choosing left DFS w/ choosing left before right) before right) 44

STEP 1: (Visit) Visit the rootSTEP 1: (Visit) Visit the root STEP 2:(go left) Go to the left STEP 2:(go left) Go to the left

subtree, if one exists, and do a subtree, if one exists, and do a preorder traversalpreorder traversal

STEP 3: Go to the right subtree, if STEP 3: Go to the right subtree, if one exists, and do a preorder one exists, and do a preorder traversal.traversal.

4.6.4,4.6.54.6.4,4.6.5

Page 24: Graph Theory TreesAlgorithms. 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

Postorder Traversal Postorder Traversal 66

RPN—Reverse Polish NotationRPN—Reverse Polish Notation Operation sign is followed by the operands Operation sign is followed by the operands

(HP calculator)(HP calculator) (2-3*4)+(4+8/2)=-2(2-3*4)+(4+8/2)=-2 Pre: +-2*34+4/82Pre: +-2*34+4/82

Preorder—look for operation sign followed Preorder—look for operation sign followed by two numbersby two numbers

Post: 234*-482/++Post: 234*-482/++– Postorder—look for two condecutive numbers Postorder—look for two condecutive numbers

followed by an operation followed by an operation By using a traversal called postorder, can By using a traversal called postorder, can

obtain the RPN for an expressionobtain the RPN for an expression

Page 25: Graph Theory TreesAlgorithms. 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

Postorder Traversal (child Postorder Traversal (child before parent, left before before parent, left before

right) right) 44

STEP 1: (Start) Go to the rootSTEP 1: (Start) Go to the root STEP 2:(go left) Go to the left subtree, if STEP 2:(go left) Go to the left subtree, if

one exists, and do a postorder traversalone exists, and do a postorder traversal STEP 3: (go right) Go to the right STEP 3: (go right) Go to the right

subtree, if one exists, and do a subtree, if one exists, and do a postorder traversal.postorder traversal.

Step 4: (Visit) Visit the rootStep 4: (Visit) Visit the root 4.6.7, 4.6.84.6.7, 4.6.8

Page 26: Graph Theory TreesAlgorithms. 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

Binary Search TreeBinary Search Tree (to determine if an element (to determine if an element aa is in a binary is in a binary

search tree)search tree) Step 1(Step 1(Start) Let V be the root of the binary Start) Let V be the root of the binary

search tree.search tree. Step 2Step 2 (compare) If a = V, then A is in the (compare) If a = V, then A is in the

tree; STOP. Else, go to Step 3tree; STOP. Else, go to Step 3 Step 3 (Step 3 (if smaller, go left) If V<=a, go to if smaller, go left) If V<=a, go to

Step 4. Otherwise, a<=VStep 4. Otherwise, a<=V– If no left child of V, a is not in tree. STOPIf no left child of V, a is not in tree. STOP– Else, V has left child L; let V=L and go to Step 2Else, V has left child L; let V=L and go to Step 2

Step 4Step 4 (if larger, go right) (if larger, go right)