Graph Theory - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Theory

Description:

A node in a rooted tree is a leaf if it is at root i (i =0) and not adjacent to ... A tree which is a subgraph of a graph G ... Binary trees and traversals 6 ... – PowerPoint PPT presentation

Number of Views:457
Avg rating:3.0/5.0
Slides: 27
Provided by: University482
Learn more at: http://pirate.shu.edu
Category:
Tags: graph | theory | trees

less

Transcript and Presenter's Notes

Title: Graph Theory


1
Graph Theory
  • Trees
  • Algorithms

2
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

3
Trees 6
  • 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 (igt0) and not adjacent to any nodes at
    level i1
  • 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

4
Trees (cont.)
   
 
5
Spanning and Rooted trees
  • Petersen Graph (find spanning treesMaple
    counttrees)
  • Rooted tree
  • Rooted tree 2


 
6
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

7
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)

8
Proof (partial--continued)
  • To show (d)?(a)
  • Assume acyclic and n-1 edges
  • RTStree(unique path betw. every node pair)
  • WTST 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) lt
    (n1n2n3nk)-1n-1
  • ??Contradiction T connected, so T is a tree

9
Uses of trees 9
  • Ex/ Huffman codes
  • ASCII-each character encoded in a 7-bit string
  • A 100 0000
  • B100 0001
  • C 100 0010
  • 1 011 0001
  • 2011 0010
  • ! 010 0001
  • 010 1010

10
Uses of trees (cont)
  • See board (BNF 15.3,15.4)

11
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 outchecking all nodes at a each level
    before moving on
  • First strategy depth-first search

12
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.

13
DFS algorithm
  • STEP 1 (Start) Pick a node u and assign it label
    1. Let k1, ? u, and ? ? and Xu.
  • STEP 2(check for completion) If ? contains each
    node of G, then StopG 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, putX,Y in ?, increment k
    to k1, assign the label k to Y, and put Y in ?.
    Replace X by Y, and go to Step 2
  • (Back up) If Xu, then Stop. G is not connected.
    Else, replace X by the predecessor of X and go to
    Step 3

14
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 ?.

15
BFS algorithm
  • STEP 1 (Start) Pick a node u and assign it label
    0. Let ? u, and ? ? and Xu.
  • STEP 2(check for completion) If ?ltn, 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
    k1, and put them in ?. For each new node with
    label k1, 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)
  • 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
  • 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.

18
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 childleft and below parent
  • Right childright 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 subtreedefined similarly

19
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.

20
Rooted Tree
    D

B A
C
21
Expression trees
  • 4.6.
  • Polish notationPolish mathematician
    Lukasiewiczno parens needed
  • RT4

22
Traversal 4
  • Traversal visit each node of a graph exactly
    once
  • BFS/DFStraversal of a connected graphnodes 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

23
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

24
Postorder Traversal 6
  • RPNReverse Polish Notation
  • Operation sign is followed by the operands (HP
    calculator)
  • (2-34)(48/2)-2
  • Pre -2344/82 Preorderlook for operation
    sign followed by two numbers
  • Post 234-482/
  • Postorderlook for two condecutive numbers
    followed by an operation
  • By using a traversal called postorder, can obtain
    the RPN for an expression

25
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

26
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 Vlta, go to Step
    4. Otherwise, altV
  • If no left child of V, a is not in tree. STOP
  • Else, V has left child L let VL and go to Step
    2
  • Step 4 (if larger, go right)
Write a Comment
User Comments (0)
About PowerShow.com