82
Data Structures and Algorithms © Ashutosh Kumar Singh By Ashutosh Kumar Singh

By Ashutosh Kumar Singh - profashutosh.comprofashutosh.com/wp-content/uploads/2019/03/DS13.pdf · Data Structures and Algorithms © Ashutosh Kumar Singh A graph is called a tree if,

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    By

    Ashutosh Kumar Singh

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Definitions

    Rooted Trees

    Depth and Height of Trees

    Binary Trees

    Spanning Trees

    Depth First Searching

    Breadth First Searching

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A graph is called a tree if, and only if, it is circuit-free and connected.

    A trivial tree is a graph that consists of a single vertex, and an empty tree is a tree that does not have any vertices or edges.

    A graph is called a forest if, and only if, it contains no simple circuits.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 4

    a b

    c

    d

    e

    G1

    a b

    cd

    G2

    e

    f

    a treeNot a tree a forest

    a

    c

    d

    b e

    G3

    f

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A tree is a collection of nodes connected by edges

    A node contains Data (e.g. an Object) References (edges) to two or more subtrees or children

    Trees are hierarchical A node is said to be the parent of its children

    (subtrees) There is a single unique root node that has no parent Nodes with no children are called leaf nodes A tree with no nodes is said to be empty

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    J

    Z A

    DRB

    LFAKQLeaves

    Nodes

    RootEdges

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    a b

    cd e f

    g h

    r

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.

    Ordered rooted trees are drawn so that the children of each internal vertex are shown in order from left to right.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 10

    A rooted tree is called a N-ary tree if every internal vertex has no more than n children. The tree is called a full N-ary tree if every internal vertex has exactly n children.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Theorem:

    A tree with n vertices has n-1 edges

    Theorem:

    A full N-ary tree with i internal vertices contains n=Ni+1 vertices.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Path:

    A connected sequence of directed edges from one node to another

    Path

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Path:

    A connected sequence of directed edges from one node to another

    Path

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Path:

    A connected sequence of directed edges from one node to another

    No Path

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Parent (X), child (Y):

    A node X is a parent of node Y if there exists a path of length 1 from X to Y

    ParentChild

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Ancestor (X), Descendent (Y):

    A node X is an ancestor of node Y (descendent) if there exists a path from X to Y

    AncestorDescendent

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Root, leaf:

    Root: a node with no parent (>1 possible)

    Leaf: a node with no children(at least 1 always)

    Root

    Leaf

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Internal node

    A node that has a parent and a child(at least 1)

    Root

    Leaf

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Siblings:

    Nodes that have same parent

    Siblings

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Subtree:

    A node and all of its descendents

    Root of

    subtree

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Subtree:

    A node and all of its descendents

    No

    subtree

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Height:

    The maximum length of a path from root of a (sub)tree to leaf (farthest leaf)

    Height: 4

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Level X:

    All nodes such that the path length from root to the nodes is of length X

    Level: 0

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Level X:

    All nodes such that the path length from root to the nodes is of length X

    Level: 1

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Level X:

    All nodes such that the path length from root to the nodes is of length X

    Level: 2

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Level X:

    All nodes such that the path length from root to the nodes is of length X

    Level: 3

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Level X:

    All nodes such that the path length from root to the nodes is of length X

    Level: 4

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    depth or level: length of the path from root to the current node

    (depth of root = 0)

    height: length of the longest path from root to any leaf empty (null) tree's height: -1 single-element tree's height: 0 tree with a root with children: 1

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A

    B C

    D E

    GF

    H

    Height – # of nodes on longest path from the root to a leaf (including root and the leaf!).

    Level – Root is at level 1, its direct children are at level 2, etc.

    Question: Can we recursively determine the height of a node?

    *Recursive definition for ‘height’:

    int height(Tree T) {

    if Tree T is empty, return 0 // base case

    else

    return 1+ max(height(TL), height(TR)) }

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    • Sibling – two nodes are siblings if they have the same parent

    • Ancestor – a node’s parent is its first ancestor, the parent of the parent is its next ancestor, etc.

    • Descendant – a node’ children are its first descendants, etc.

    • path: a sequence of nodes n1, n2, … , nk such that ni is the parent of ni+1 for 1 i < k

    • in other words, a sequence of hops to get from one node to another the length of a path is the number of edges in the path, or 1 less than the number of nodes in it

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    leaves

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A binary tree is a rooted tree in which every internal vertex has at most two children. Each child in a binary tree is designated either a left child or a right child(but not both), and an internal vertex has at most one left and one right child.

    A full binary tree is a binary tree in which each internal vertex has exactly two children.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Given an internal vertex v of a binary tree T, the left subtree of v is the binary tree whose root is the left child of v, whose vertices consist of the left child of v and all its descendants, and whose edges consist of all those edges of T that connect the vertices of the left subtree together. The right subtree of v is defined analogously.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 36

    Theorem: If k is a positive integer and T is a full binary tree with k internal vertices, then T has a total of 2k + 1 vertices and has k + 1 terminal vertices.

    Theorem: If T is a binary tree that has t terminal vertices and height h, then

    t 2h

    Equivalently,

    log2 t h

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Minimum number of nodes in a binary tree whose height is h.

    At least one node at each of first h levels.

    minimum number of nodes is h

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    All possible nodes at first h levels are present.

    Maximum number of nodes

    = 1 + 2 + 4 + 8 + … + 2h-1 = 2h - 1

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Let n be the number of nodes in a binary tree whose height is h.

    h

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A full binary tree of a given height h has 2h – 1 nodes.

    Height 4 full binary tree.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Number the nodes 1 through 2h – 1.

    Number by levels from top to bottom.

    Within a level number from left to right.

    1

    2 3

    4 5 6 7

    8 9 10 11 12 13 14 15

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Parent of node i is node i / 2, unless i = 1.

    Node 1 is the root and has no parent.

    1

    2 3

    4 5 6 7

    8 9 10 11 12 13 14 15

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Left child of node i is node 2i, unless 2i > n, where n is the number of nodes.

    If 2i > n, node i has no left child.

    1

    2 3

    4 5 6 7

    8 9 10 11 12 13 14 15

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes.

    If 2i+1 > n, node i has no right child.

    1

    2 3

    4 5 6 7

    8 9 10 11 12 13 14 15

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Start with a full binary tree that has at least n nodes.

    Number the nodes as described earlier.

    The binary tree defined by the nodes numbered 1through n is the unique n node complete binary tree.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Complete binary tree with 10 nodes.

    1

    2 3

    4 5 6 7

    8 9 10 11 12 13 14 15

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Array representation.

    Linked representation.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Number the nodes using the numbering scheme for a full binary tree. The node that is numbered i is stored in tree[i].

    tree[]0 5 10

    a b c d e f g h i j

    b

    a

    c

    d e f g

    h i j

    1

    2 3

    4 5 6 7

    8 9 10

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    An n node binary tree needs an array whose length is between n+1 and 2n.

    a

    b

    1

    3

    c7

    d15

    tree[]0 5 10

    a - b - - - c - - - - - - -

    15

    d

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Each binary tree node is represented as an object whose data type is BinaryTreeNode.

    The space required by an n node binary tree is n * (space required by one node).

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    a

    cb

    d

    f

    e

    g

    hleftChildelementrightChild

    root

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Determine the height.

    Determine the number of nodes.

    Display the binary tree.

    Evaluate the arithmetic expression represented by a binary tree.

    Obtain the infix form of an expression.

    Obtain the prefix form of an expression.

    Obtain the postfix form of an expression.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Evaluation of the arithmetic expression by a binary tree.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Many binary tree operations are done by performing a traversal of the binary tree.

    In a traversal, each element of the binary tree is visitedexactly once.

    During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 55

    Ordered rooted trees are often used to store information.

    It is often need to visit each vertex of an ordered rooted tree to access data.

    Procedures for systematically visiting each vertex of an ordered rooted tree are called traversal algorithms.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    There are three most commonly used traversal algorithms:

    Preorder traversal

    Inorder traversal

    Postorder traversal

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Let T be an ordered rooted tree with root r. If T consists of only r, then r is the preorder traversal of T. Otherwise, suppose that T1, T2, ……Tn are the subtrees at r from left to right in T. The preorder traversal of T begins by visiting r. It continues by traversing T1 in preorder, then T2 in preorder and so on, until Tn is traversed in preorder.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    The preorder traversal of T: racdghjbefk

    a b

    c d e f

    g h

    r

    kj

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Let T be an ordered rooted tree with root r. If T consists of only r, then r is the inorder traversal of T. Otherwise, suppose that T1, T2, ……Tn are the subtrees at r from lest to right in T. The inorder traversal of T begins by traversing T1 in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder and so on, until Tn is traversed in inorder.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    The inorder traversal of T: cagdhjrebkf

    a b

    c d e f

    g h

    r

    kj

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Let T be an ordered rooted tree with root r. If T consists of only r, then r is the postorder traversal of T. Otherwise, suppose that T1, T2, ……Tn are the subtrees at r from lest to right in T. The postorder traversal of T begins by traversing T1 in postorder, the T2 in postorderand so on, until Tn is traversed in postorder and ends by visiting r.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 62

    The postorder traversal of T:

    cghjdaekfbr

    a b

    c d e f

    g h

    r

    kj

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 63

    Consider the following binary tree T.

    a

    bc

    d

    e

    f

    ghi

    •Find the depth of T.•Traverse T using the preorder algorithm.•Traverse T using the inorder algorithm.•Traverse T using the postorder algorithm.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A spanning tree for a graph G is a subgraph of G that contains every vertex of G and is a tree.

    Proposition:

    1. Every connected graph has a spanning tree.

    2. Any two spanning trees for a graph have thesame number of edges.

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    The idea of breadth-first search (BFS) is to process all the vertices on a given level before moving to the next level.

    A graph with its spanning tree using BFS method

    (typically “short and bushy”)

    A

    H

    B

    F

    C D

    E

    G

    A B

    DC

    E F

    HG

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A

    H

    B

    F

    C D

    E

    G

    A B

    DC

    E F

    HG

    A graph with its spanning tree using DFS method

    (typically “long and stringy”)

    • The idea of Depth-first search (DFS) is to explore along each branch as far as possible before backtracking

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem)

    For some problems, any goal node is acceptable (Nor J); for other problems, you want a minimum-depth goal node, that is, a goal node nearest the root (only J)

    L M N O P

    G

    Q

    H JI K

    FED

    B C

    A

    Goal nodes

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 68

    A depth-first search (DFS) explores a path all the way to a leaf before backtrackingand exploring another path

    For example, after searching A, then B, then D, the search backtracks and tries another path from B

    Node are explored in the order A B D E H L M N I O P C F G J K Q

    N will be found before J

    L M N O P

    G

    Q

    H JI K

    FED

    B C

    A

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 69

    Put the root node on a stack;while (stack is not empty) {

    remove a node from the stack;if (node is a goal node) return success;put all children of node onto the stack;

    }return failure;

    At each step, the stack contains some nodes from each of a number of levels The size of stack that is required depends on the

    branching factor b While searching level n, the stack contains

    approximately b*n nodes

    When this method succeeds, it doesn’t give the path

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    search(node):if node is a goal, return success;for each child c of node {

    if search(c) is successful, return success;}return failure;

    The (implicit) stack contains only the nodes on a path from the root to a goal The stack only needs to be large enough to hold the

    deepest search path When a solution is found, the path is on the (implicit)

    stack, and can be extracted as the recursion “unwinds”

    print node and

    print c and

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 71

    A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away

    For example, after searching A, then B, then C, the search proceeds with D, E, F, G

    Node are explored in the order A B C D E F G H I J K L M N O P Q

    J will be found before NL M N O P

    G

    Q

    H JI K

    FED

    B C

    A

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Put the root node on a queue;while (queue is not empty) {

    remove a node from the queue;if (node is a goal node) return success;put all children of node onto the queue;

    }return failure;

    Just before starting to explore level n, the queue holds allthe nodes at level n-1

    In a typical tree, the number of nodes at each level increases exponentially with the depth

    Memory requirements may be infeasible

    When this method succeeds, it doesn’t give the path

    There is no “recursive” breadth-first search equivalent to recursive depth-first search

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Depth-first searching: Put the root node on a stack;

    while (stack is not empty) {remove a node from the stack;if (node is a goal node) return success;put all children of node onto the stack;

    }return failure;

    Breadth-first searching: Put the root node on a queue;

    while (queue is not empty) {remove a node from the queue;if (node is a goal node) return success;put all children of node onto the queue;

    }return failure;

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 74

    When a breadth-first search succeeds, it finds a minimum-depth (nearest the root) goal node

    When a depth-first search succeeds, the found goal node is not necessarily minimum depth

    For a large tree, breadth-first search memory requirements may be excessive

    For a large tree, a depth-first search may take an excessively long time to find even a very nearby goal node

    How can we combine the advantages (and avoid the disadvantages) of these two search techniques?

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 75

    Depth-first searches may be performed with adepth limit:

    boolean limitedDFS(Node node, int limit, int depth) {if (depth > limit) return failure;if (node is a goal node) return success;for each child of node {

    if (limitedDFS(child, limit, depth + 1))return success;

    }return failure;

    }

    Since this method is basically DFS, if it succeeds then the path to a goal node is in the stack

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh 76

    limit = 0;found = false;while (not found) {

    found = limitedDFS(root, limit, 0);limit = limit + 1;

    }

    This searches to depth 0 (root only), then if that fails it searches to depth 1, then depth 2, etc.

    If a goal node is found, it is a nearest node and the path to it is on the stack Required stack size is limit of search depth (plus 1)

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Nodes at each level

    1

    2

    4

    8

    16

    32

    64

    128

    Nodes searched by DFS

    1

    +2 = 3

    +4 = 7

    +8 = 15

    +16 = 31

    +32 = 63

    +64 = 127

    +128 = 255

    Nodes searched by iterative DFS

    1

    +3 = 4

    +7 = 11

    +15 = 26

    +31 = 57

    +63 = 120

    +127 = 247

    +255 = 502

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Nodes at each level

    1

    4

    16

    64

    256

    1024

    4096

    16384

    Nodes searched by DFS

    1

    +4 = 5

    +16 = 21

    +64 = 85

    +256 = 341

    +1024 = 1365

    +4096 = 5461

    +16384 = 21845

    Nodes searched by iterative DFS

    1

    +5 = 6

    +21 = 27

    +85 = 112

    +341 = 453

    +1365 = 1818

    +5461 = 7279

    +21845 = 29124

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    When searching a binary tree to depth 7: DFS requires searching 255 nodes Iterative deepening requires searching 502 nodes Iterative deepening takes only about twice as long

    When searching a tree with branching factor of 4 (each node may have four children): DFS requires searching 21845 nodes Iterative deepening requires searching 29124 nodes Iterative deepening takes about 4/3 = 1.33 times as long

    The higher the branching factor, the lower the relative cost of iterative deepening depth first search

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Breadth-first search (BFS) and depth-first search (DFS) are the foundation for all other search techniques

    We might have a weighted tree, in which the edges connecting a node to its children have differing “weights” We might therefore look for a “least cost” goal

    The searches we have been doing are blind searches, in which we have no prior information to help guide the search If we have some measure of “how close” we are to a goal node, we

    can employ much more sophisticated search techniques

    We will not cover these more sophisticated techniques

    Searching a graph is very similar to searching a tree, except that we have to be careful not to get caught in a cycle

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Definitions

    Rooted Trees

    Depth and Height of Trees

    Binary Trees

    Spanning Trees

    Depth First Searching

    Breadth First Searching

  • Data Structures and Algorithms

    © Ashutosh Kumar Singh

    Rosen Kenneth H. Discrete Mathematics and its Applications, 5th Edition. McGraw-Hill.