Top Banner

of 94

Slides on Data Structures tree and graph

Jun 04, 2018

Download

Documents

tedy tedy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 Slides on Data Structures tree and graph

    1/94

    Non inear

  • 8/13/2019 Slides on Data Structures tree and graph

    2/94

    A brief introduction on Trees

    Graphs

    Review and Examine key properties of Trees and variants

    Graph and variants

    Discuss usage for solving important problems

    (search, sort, selection).

    Searching And Sorting Algorithms

  • 8/13/2019 Slides on Data Structures tree and graph

    3/94

    linear data structures

    oA linear data structure traverses the data elements

    sequentially, in which only one data element can directly

    be reached. Ex: Arrays and Linked Lists,

    Non-linear data structures

    owhereas in non-Linear data structure every data item is

    attached to several other data items in a way that is

    specific for reflecting relationships. The data items are notarranged in a sequential structure. Ex: Trees, Graphs

  • 8/13/2019 Slides on Data Structures tree and graph

    4/94

    A tree is a collection of nodes and directed

    edges, satisfying the following properties:

    There is one specially designated node called the

    root, which has no edges pointing to it. Every node except the roothas exactly one edge

    pointing to it.

    There is a uniquepath (of nodes and edges) from

    the rootto each node.

  • 8/13/2019 Slides on Data Structures tree and graph

    5/94

    Node user-defined data structure that that containspointers to data and pointers to other nodes: RootNode from which all other nodes descend

    Parenthas child nodes arranged in sub-trees.

    Childnodes in a tree have 0 or more children.

    Leafnode without descendants

    Degree# of direct children a tree/sub-tree has.

    In-degree:# of branches directed toward the node

    Out-degree:# of branches directed away

    Degree of node:. Sum (in-degree and out-degree)

    Degree of tree: maximum degree of any node

    Forest:Collection of trees

    Path:sequence of adjacent nodes

  • 8/13/2019 Slides on Data Structures tree and graph

    6/94

    Height# of edges on the

    longestpath from the root to a leaf.

    LevelRoot is at level 0, its

    direct children are at level 1, etc.

    Recursive definition for height:1+ max(height(TL), height(TR))

    A

    B C

    D E

    GF

    H

  • 8/13/2019 Slides on Data Structures tree and graph

    7/94

    If an edge goes from node ato node b, then ais called

    the parentof b, and bis called a childof a.

    Children of the same parent are called siblings.

    If there is a path from a to b, then a is called anancestorof b, and bis called a descendentof a.

    A node with all of its descendants is called a subtree.

    If a node has no children, then it is called a leafof the

    tree. If a node has no parent (there will be exactly one of

    these), then it is the rootof the tree.

  • 8/13/2019 Slides on Data Structures tree and graph

    8/94

    A

    B C

    D E

    F

    G H

    I

    J K

    A is the root D, E, G, H, J & K are

    leaves

    B is the parentof D, E &F

    D, E & F are siblingsand childrenof B

    I, J & K are descendantsof B

    A & B are ancestorsof I

  • 8/13/2019 Slides on Data Structures tree and graph

    9/94

    An acyclic connected graph where each node has zero

    or more children nodes and there is at most oneparent

    node, and at most one node has no parents, which is

    root node of the tree.

    Figure : General Tree

  • 8/13/2019 Slides on Data Structures tree and graph

    10/94

    A balanced binary tree is commonly defined as a

    binary tree in which the height of the two sub trees of

    every node never differs by more than 1.

    Figure : A Balanced TreeFigure : An Unbalanced Tree

  • 8/13/2019 Slides on Data Structures tree and graph

    11/94

    Heap is an ordered balanced binary tree in which the

    value of the node at root of any subtree can be greater

    than, less than or equal to the value of either of its

    children.

    Figure (a): Max-Heap Figure (b): Min-Heap

  • 8/13/2019 Slides on Data Structures tree and graph

    12/94

    A binary tree in which each node is having either

    only left sub-tree, either only right sub-tree or no sub-

    tree is called as left skewed or right skewed binary

    tree respectively.

    (a) Left Skewed binary tree

    (b) Right Skewed Binary tree

    Figure (a) (b)

  • 8/13/2019 Slides on Data Structures tree and graph

    13/94

    A binary tree is a tree in which each node can have

    maximum of two children.Thus each node can have no, one or two child.

    pointers are used to identify whether it is a left or a right child

    Intuitively, a binary treeis a treein which each node

    has no more than two children.

    Figure : Binary Tree

    (These two binarytrees are distinct.)

  • 8/13/2019 Slides on Data Structures tree and graph

    14/94

    A binary search tree is a binary tree in which each node, n,

    has a Comparable key (and an associated value)satisfying the

    following restriction on the key in any node :

    nsvalue is > all values in its left subtree, TL,

    nsvalue is < all values in its right sub-tree, TR, and TLand TRare both binary search trees.

    John

    PeterBrenda

    Amy Mary Tom

    21

    2

    3

    55

    34

    135

    8

    In other words, can weput non-hierarchical

    data into a tree. We

    will study Binary

    Search Trees later.

  • 8/13/2019 Slides on Data Structures tree and graph

    15/94

    A binary tree is ful l if it has no missing nodes.

    It is either empty.

    Otherwise, the rootssub-trees are fullbinary trees

    of height h1.If not empty, each node has 2 children, except the

    nodes at level hwhich have no children.

    Contains a total of 2h+1-1 nodes (how many leaves?)

    This term is

    ambiguous, some

    indicate that each

    node is either full or

    empty.

  • 8/13/2019 Slides on Data Structures tree and graph

    16/94

    A binary tree of height his completeif it is fulldown to

    level h1, and level his filled from left to right.

    All nodes at level h2 and above have 2 children each,

    If a node at level h1 has children, all nodes to its left

    at the same level have 2 children each, and

    If a node at level h1 has 1 child, it is a left child.

  • 8/13/2019 Slides on Data Structures tree and graph

    17/94

    A binary tree is balancedif the difference in height betweenany nodesleft and right sub-tree is 1.

    Note that:

    A full binary tree is also complete. A complete binary tree is not always full. Full and complete binary trees are also balanced. Balanced binary trees are not always full or complete.

  • 8/13/2019 Slides on Data Structures tree and graph

    18/94

    Complete and Balanced

    Not Balanced, Why?

  • 8/13/2019 Slides on Data Structures tree and graph

    19/94

    The number of edges in a tree is n-1. The number of nodes nin a full binary tree is: n = 2h + 1 1

    where his the height of the tree. The number of nodes nin a complete binary tree is:

    minimum: n = 2h

    maximum: n = 2

    h + 1

    1where his the height of the tree. The number of nodes nin a full or perfect binary tree is:

    n = 2L 1whereLis the number of leaf nodes in the tree.

    The number of leaf nodes nin a full or perfect binary tree is: n = 2hwhere his the height of the tree.

    The number of leaf nodes in a Complete Binary Tree with nnodes is UpperBound(n/ 2). For any non-empty binary tree with n0leaf nodes and n2nodes of

    degree 2, n0= n2+ 1.

  • 8/13/2019 Slides on Data Structures tree and graph

    20/94

    Basic Idea:

    Instead of using pointersto the left and right child of a

    node, use indices into an array of nodes representing

    the binary tree.

    Also, use variable freeas an index to the first position

    in the array that is available for a new entry. Use either

    the left or r ight child indices to indicate additional,

    available positions. Together, the list of available positions in the array is

    called the f ree list.

  • 8/13/2019 Slides on Data Structures tree and graph

    21/94

    Index Item Left

    Child

    Right

    Child0 Jane 1 2

    1 Bob 3 4

    2 Tom 5 -1

    3 Alan -1 -1

    4 Ellen -1 -1

    5 Nancy -1 -1

    6 ? -1 7

    7 ? -1 8

    8 ? -1 9

    9 . . . . . . . . .

    root0

    free

    6

    Jane

    Alan

    TomBob

    NancyEllen

  • 8/13/2019 Slides on Data Structures tree and graph

    22/94

    Index Item Left

    Child

    Right

    Child

    0 Jane 1 2

    1 Bob 3 4

    2 Tom 5 -1

    3 Alan -1 -1

    4 Ellen -1 -1

    5 Nancy 6 -1

    6 Mary -1 -1

    7 ? -1 8

    8 ? -1 9

    9 . . . . . . . . .

    root0

    free

    7

    *Mary Added under Nancy.

    Jane

    Alan

    TomBob

    NancyEllen

    Mary

  • 8/13/2019 Slides on Data Structures tree and graph

    23/94

    Index Item LeftChild

    RightChild

    0 Jane 1 2

    1 Bob 3 -1

    2 Tom 5 -13 Alan -1 -1

    4 ? -1 7

    5 Nancy 6 -1

    6 Mary -1 -17 ? -1 8

    8 ? -1 9

    9 . . . . . . . . .

    root0

    free

    4

    *Ellen deleted.

    Jane

    Alan

    TomBob

    Nancy

    Mary

  • 8/13/2019 Slides on Data Structures tree and graph

    24/94

    1

    2 3

    4 75 6

    Let i, 1 < i< n, be the number assigned to an element of a complete binary tree.

  • 8/13/2019 Slides on Data Structures tree and graph

    25/94

    1

    2 3

    4 6

    61 2 3

    5 7

  • 8/13/2019 Slides on Data Structures tree and graph

    26/94

  • 8/13/2019 Slides on Data Structures tree and graph

    27/94

    Array-based representations allow for efficient traversal.Consider the node at index i.

    Left Child is at index 2i+1.

    Right Child is at index 2i+2.

    Parent is at floor( (i-1)/2 ).

  • 8/13/2019 Slides on Data Structures tree and graph

    28/94

    1

    3

    7

    1 3 7

    Drawbackof array-based trees: Example has only 3nodes, but uses 2h+1-1 array cells to store it

    However, if the array

    is just numbers or

    pointers, not a huge

    concern.

  • 8/13/2019 Slides on Data Structures tree and graph

    29/94

    We can encode an n-ary tree as a binary tree, byhaving a linked-list of children. Hence still two

    pointers, one to the first child and one to the next

    sibling. Kinda rotates the tree.

    http://en.wikipedia.org/wiki/File:N-ary_to_binary.svg
  • 8/13/2019 Slides on Data Structures tree and graph

    30/94

    Insertion In BST Deleting from a BST

    Binary Search Tree Traversing

    Depth-first Traversal

    Preorder In-order

    Post-order

    Breadth-First Traversal

    Level order

    Constructing Tree for Given in-Order & Pre/Post Order Traversal of Tree

    Searching and Sorting using Binary Search Tree

  • 8/13/2019 Slides on Data Structures tree and graph

    31/94

    Figure. Insert a new element whose value is 42

    set x=42, p=36 and found =0;

    While (p!=null and !found)

    parent = p;

    if (p->Info = x) then

    Set found = 1;

    Else if (x < p->Info) then

    Set p = p->lchild;

    Else

    Set p = p->rchild;

    [end of while loop at Step2]

    1stiteration

    Set p = 40 //p->rchild;

    2nditeration

    Set p = 44 //p->rchild;

    3rditeration

    if (42 < 44) then

    Set p = 42 //p->lchild;

  • 8/13/2019 Slides on Data Structures tree and graph

    32/94

  • 8/13/2019 Slides on Data Structures tree and graph

    33/94

  • 8/13/2019 Slides on Data Structures tree and graph

    34/94

    Depth-first Traversal

    Preorder

    Inorder

    Postorder

    Breadth-First Traversal

    Level order

  • 8/13/2019 Slides on Data Structures tree and graph

    35/94

    Basic Idea:

    1) Visitthe root.

    2) Recursively invoke preorderon the left sub-tree.3) Recursively invoke preorderon the right sub-tree.

  • 8/13/2019 Slides on Data Structures tree and graph

    36/94

  • 8/13/2019 Slides on Data Structures tree and graph

    37/94

    Basic Idea:

    1) Recursively invoke inorderon the left sub-tree.

    2)Visit

    the root.3) Recursively invoke inorderon the right subtree.

  • 8/13/2019 Slides on Data Structures tree and graph

    38/94

    InorderResult: 10, 20, 30, 40, 50, 60, 70

    60

    4010

    7020

    30 50

    6

    4

    3

    1

    2 7

    5

  • 8/13/2019 Slides on Data Structures tree and graph

    39/94

    Basic Idea:

    1) Recursively invoke postorderon the left sub-tree.

    2) Recursively invokepostorder

    on the right subtree.3) Visitthe root.

  • 8/13/2019 Slides on Data Structures tree and graph

    40/94

    PostorderResult: 10, 30, 50, 40, 20, 70, 60

    60

    4010

    7020

    30 50

    7

    4

    2

    1

    5 6

    3

  • 8/13/2019 Slides on Data Structures tree and graph

    41/94

    f

    c j

    a d h k

    i

    Visit the tree in left-to-right, by level, order: Visit the root node and put its children in a queue (left to right).

    Dequeue, visit, and put dequeued nodes children into the queue.

    Repeat until the queue is empty. Level order traversal of

    the figure given below

    fcjadhki

  • 8/13/2019 Slides on Data Structures tree and graph

    42/94

    Basic Ideafor a Nonrecursive,InorderTraversal:1) Push a pointer to the rootof the binary tree onto a stack.

    2) Follow leftChild pointers, pushing each one onto the stack,

    until aNULLleftChildpointer is found.

    3) Process (visit) the item in this node.

    4) Get the nodesrightChildpointer:

    If it is notNULL, then push it onto the stack, and return to

    step 2 with the leftChildpointer of this rightChild.

    If it is NULL, then pop a node pointer from the stack, andreturn to step 3. If the stack is empty (so nothing could be

    popped), then stopthe traversal is done.

  • 8/13/2019 Slides on Data Structures tree and graph

    43/94

    ExampleA binary tree T has 9 nodes. The inorder and preorder traversals of T yield thefollowing sequence of nodes

    Inorder E A C K F H D B G

    Preorder F A E K C D H G B

    Draw the tree.

    The tree T is drawn from its root downward in different stages as follows.

    The root of T is obtained by choosing the first node in its preorder. Thus F isthe root of T

    The inorder of T to find the nodes in the left subtree T of F. Thus T1 consists ofnodes E, A, C, K then. Then the left child of F is obtained by choosing the first

    node in the preorder of T1. Thus A is the left child of F. Similarly, the right subtree T2 of F consists of the nodes H, D, B, and G. D is

    the right child of F. Repeating the above process with each new node is givenbelow.

  • 8/13/2019 Slides on Data Structures tree and graph

    44/94

  • 8/13/2019 Slides on Data Structures tree and graph

    45/94

    Figure Construction of binary tree from inorder and preorder sequence

    The postorder sequence of tree is visit left subtree, visit right subtree then visit rooted node.

    E C K A H B G D F

  • 8/13/2019 Slides on Data Structures tree and graph

    46/94

    Tree finite, non-empty set of elements with a root with allremaining elements in sub-trees Real World applicationsBinary space partitioning trees (BSP)

    efficient method for determining visibility relationships betweena static group of 3d polygons from an arbitrary viewpoint. Aviewpoint can be composed of a clusters of polygons. A planecan be found that separates on set of clusters from another thussome are visible and some arent. The tree is rooted at the firstpartitioning plane chose, and the internal nodes describe thepartitioning planes and the leaves describe the regions of space.VIDEO GAMESDOOM XXX, Quake X, etc

    Compiler constructiondealing with syntax analysis, symboltable construction,arithmetic expression manipulation. You are a child of two parents, who in turn came from two

    parents.Hierarchical!!!

  • 8/13/2019 Slides on Data Structures tree and graph

    47/94

    Discuss reasons forusing treesvs. using lists

    Efficient retrieval

    traversal of listvs. traversal of a tree.

    Sorting

    treevs. list

  • 8/13/2019 Slides on Data Structures tree and graph

    48/94

    Definition A graph G consists of two sets V and E.

    o The set V is a finite, non-empty set of vertices.o The set E is a set of pairs of vertices, these pairs are called edges.

    Graph G can be represent a set of G =(V,E)

    A graph is nodesjoined by edge i.e. A set of nodes V anda set of edges E

    A node is defined by its name or label. An edge is defined by the two nodes which it connects,

    plus optionally:An order of the nodes (direction)A weight or cost (usually a number)

  • 8/13/2019 Slides on Data Structures tree and graph

    49/94

    Graph G= (V, E) V= set of vertices E= set of edges (VV)

    Types of graphs Undirected:edge (u, v) = (v, u); for all v, (v, v) E(No

    self loops.) Directed:(u, v) is edge from uto v, denoted as u v.

    Self loops are allowed. Weighted: each edge hasan associated weight, given

    by a weight function w : ER. Dense:|E| |V|2.(# of edges/#of nodes is large) Sparse:|E|

  • 8/13/2019 Slides on Data Structures tree and graph

    50/94

    Path:- (u1,u2,u3

    v) Vand(u1,u2),(u2,u3),uk,v) Eare in G(E).Degreei. I n undirected graph

    A nodesdegree is the number of its edges

    i i . In directed graphI ndegree:-is the # of edges for which v is head. Outdegree:- is the # of edges for which v is tail.

    length:the number of edges in the path

    cost:the sum of the weights on each edge in the pathcycle:a path that starts and finishes at the same node An acyclicgraph contains no cycles

  • 8/13/2019 Slides on Data Structures tree and graph

    51/94

    Two nodes are adjacent if they are connected by anedge.

    If (u, v) E, then vertex vis adjacentto vertex u.

    Adjacency relationship is:

    Symmetric if G is undirected. Not necessarily so, if Gis directed.

    If Gis strongly connected:

    There is a path between every pair of vertices.

    |E| |V|1.

    Furthermore, if |E| = |V| 1, then Gis a tree.

  • 8/13/2019 Slides on Data Structures tree and graph

    52/94

    For example :-(undirected graph) in the first Figure V(G)={1,2,3,4}

    E(G)={(1,2),(2,4),(4,3),(3,1)}

    For example :-( directed graph) See the second Figure. V(G)={1,2,3,4}

    E(G)={(1,2),(2,1)(2,4)(4,2),(4,3)(3,4),(3,1)(1,3)}

    Figure Undirected graph (unweighted)Figure Directed graph (unweighted)

  • 8/13/2019 Slides on Data Structures tree and graph

    53/94

    Two standard ways. Adjacency Lists.

    Adjacency Matrix.

    a

    d

    b

    a

    d

    b1 2

    3 4

    1 2 3 41 0 1 1 1

    2 1 0 1 03 1 1 0 14 1 0 1 0

    abcd

    b

    a

    d

    d c

    c

    a b

    a c

  • 8/13/2019 Slides on Data Structures tree and graph

    54/94

    Consists of an array Adjof |V| lists. One list per vertex.

    For uV, Adj[u] consists of all vertices adjacent to u.

    a

    d

    b abcd

    b

    c

    d

    d c

    a

    d

    b

    If weighted, store weightsalso in adjacency lists.

    abcd

    b

    a

    d

    d c

    c

    a b

    a c

  • 8/13/2019 Slides on Data Structures tree and graph

    55/94

    |V| |V| matrix A. Number vertices from 1 to |V| in some arbitrary manner.

    Ais then given by:

    otherwise0

    ),(if1],[

    EjiajiA ij

    a

    d

    b1 2

    3 4

    1 2 3 41 0 1 1 12 0 0 1 03 0 0 0 14 0 0 0 0

    a

    d

    b1 2

    3 4

    1 2 3 41 0 1 1 12 1 0 1 03 1 1 0 14 1 0 1 0

    A= ATfor undirected graphs.

  • 8/13/2019 Slides on Data Structures tree and graph

    56/94

    In the given graph G=(V,E), we want to visit all vertices in Gthat are reachable from vertex v, where v V. Problems with the Graph Traversal:

    I. No First Node. So there must be a way to find out the starting nodeof the graph. User can enter the starting point or there can be severalother methods to find out the starting point.

    II. When traversing a graph, we must be careful to avoid going round incircles.We do this by marking the vertices which have already beenvisited. List of visited nodes can also be maintained.

    III. No natural order among the successor of a particular node.IV. Nodes which are not connected or to which there is no path.

    We have two common search methods:1. Breadth first search2. Depth first search

  • 8/13/2019 Slides on Data Structures tree and graph

    57/94

    Breadth First Search It is based on FIFO system using a Queue . Depth First Search It is based on LIFO system using a Stack .

    Figure Undirected graph, for finding the breadth first search and depth first search

  • 8/13/2019 Slides on Data Structures tree and graph

    58/94

    The steps of over search follow as start node 11. initially, add 1 to queue as follows

    Front=1 Rear =1 Queue:12. Delete front 1 and add adjacent nodes of 1 which

    is not visited.

    Front= 2 Rear=5 Queue:2, 3, 4, 5

    3. Delete front 2 and add adjacent nodes of 2 which

    is not visited.

    Front=3 Rear=6 Queue:3, 4, 5, 6

    4. Delete front 3 and add adjacent nodes of 3 which

    is not visited.

    Front=4 Rear=7 Queue:4, 5, 6, 7

  • 8/13/2019 Slides on Data Structures tree and graph

    59/94

    5. Delete front 4 and add adjacent nodes of 4 which

    is not visited.

    Front=5 Rear=8 Queue:5, 6, 7, 86. Delete front 5 from queue and add to queue

    adjacent nodes of 5 which is not visited.

    Front=6 Rear=8 Queue:6, 7, 8

    7. Delete front 6 from queue and add to queue

    adjacent nodes of 6 which is not visited.

    Front=7 Rear=9 Queue:7, 8, 9

    8. Delete front 7 from queue and add to queue

    adjacent nodes of 7 which is not visited.

    Front=8 Rear=9 Queue:8, 9

  • 8/13/2019 Slides on Data Structures tree and graph

    60/94

    9. Delete front 8 from queue and add to queue

    adjacent nodes of 8 which is not visited.

    Front=9 Queue:9

    Rear=9

    10. Delete front 9 from queue and add to queue

    adjacent nodes of 9 which is not visited.

    Front=0 Queue:0

    Rear=0Breadth first search is 1, 2, 3, 4, 5, 6, 7, 8, 9

  • 8/13/2019 Slides on Data Structures tree and graph

    61/94

    The steps of over search follow as start node 11 Initially, push 1 onto stack as follows

    Stack: 1

    2 Pop top 1 and push all adjacent nodes of 1 which is notvisited .

    Stack: 2, 3, 4, 5

    3 Pop top 5 and push all adjacent nodes of 5 which is not

    visited.

    Stack : 2, 3, 4

    4 pop top 4 and push all adjacent nodes of 4 which is notvisited.

    Stack : 2, 3, 7, 8

    5 pop top 8 and push all adjacent nodes of 8 which is not

    visited.

    Stack : 2,3,7

  • 8/13/2019 Slides on Data Structures tree and graph

    62/94

    6 pop top 7 and push all adjacent nodes of 7 which is not

    visited.

    Stack : 2,3,6

    7 pop top 6 and push all adjacent nodes of 6 which is notvisited.

    Stack : 2,3,98 pop top 9 and push all adjacent nodes of 9 which is not

    visited.

    Stack : 2,39 pop top 3 and push all adjacent nodes of 3 which is not

    visited.

    Stack : 210 pop top 2 and push all adjacent nodes of 2 which is not

    visited.

    Stack : empty

    Depth first search sequence is: 1, 5, 4, 8, 7, 6, 9, 3, 2

  • 8/13/2019 Slides on Data Structures tree and graph

    63/94

    Electrical network problems Electrical networkanalysis and synthesis are mainly the study of

    network topology

    Topological sort A topological sort of a graph can

    be viewed as an orderings of its vertices along a

    horizontal lineso that all directed edges go from left

    to right.

    Directed acyclic graphs are used in manyapplications to indicate precedence among events

    such as code optimization techniques of complier.

  • 8/13/2019 Slides on Data Structures tree and graph

    64/94

    Shortest path A path from source vertex vto w is shortest path if there is shortest path

    from v to u and u to w with lower costs. The

    shortest paths are not necessarily unique.The most common shortest path problem:-

    Traveling salesman.

    Airline, The shortest path find which routeprovide minimum air fair total time of flights.

  • 8/13/2019 Slides on Data Structures tree and graph

    65/94

    Minimum spanning tree (MST) A spanning treefor a graph, G = (V, E), is a sub-graph of G that is a

    tree and contains all the vertices of G.

    In a weighted graph, the weight of a graph is the sum

    of the weights of the edges of the graph. A MST for a

    weighted/cost graph is a spanning tree with minimum

    cost.

    There are many applications where minimumspanning tree is needed

  • 8/13/2019 Slides on Data Structures tree and graph

    66/94

    To find cheapest way to connect a set of terminals,cities, electrical, electronic components of a circuit,

    computers, or premises by using roads, wires or

    wireless, or telephone lines.

    The solution is a MST, which has an edge for eachpossible connection weighted by the cost of that

    connection.

    The routing problems to find path among the system

    over Internet, also need MST.

    Prims and Kruskals algorithm is the most common

    solution for MST.

  • 8/13/2019 Slides on Data Structures tree and graph

    67/94

    Searching AlgorithmsSequential SearchBinary Search

    Sorting AlgorithmsBubble SortInsertion SortQuick Sort

  • 8/13/2019 Slides on Data Structures tree and graph

    68/94

    ExampleSuppose we have an array of integers.

    Lets search for the number 3. We start at from

    beginning and check the first element in the array. Is it3?

  • 8/13/2019 Slides on Data Structures tree and graph

    69/94

    ExampleSuppose we have an array of integers.

    Lets search for the number 3. We start at from

    beginning and check the first element in the array. Is it3?

  • 8/13/2019 Slides on Data Structures tree and graph

    70/94

    ExampleSuppose we have an array of integers.

    Lets search for the number 3. We start at from

    beginning and check the first element in the array. Is it3?

  • 8/13/2019 Slides on Data Structures tree and graph

    71/94

    ExampleSuppose we have an array of integers.

    Lets search for the number 3. We start at from

    beginning and check the first element in the array. Is it3?

  • 8/13/2019 Slides on Data Structures tree and graph

    72/94

    ExampleSuppose we have an array of integers.

    Lets search for the number 3. We start at from

    beginning and check the first element in the array. Is it3?

  • 8/13/2019 Slides on Data Structures tree and graph

    73/94

    ExampleSuppose the following

    array of integers is given.

    2 6 7 34 76 123 234 567 677 986

    We want to seek the

    value 123 from this array.

  • 8/13/2019 Slides on Data Structures tree and graph

    74/94

  • 8/13/2019 Slides on Data Structures tree and graph

    75/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass

  • 8/13/2019 Slides on Data Structures tree and graph

    76/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9

  • 8/13/2019 Slides on Data Structures tree and graph

    77/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9 1 6 4 3 9 -> 1 4 63 9

  • 8/13/2019 Slides on Data Structures tree and graph

    78/94

  • 8/13/2019 Slides on Data Structures tree and graph

    79/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9 1 6 4 3 9 -> 1 4 63 9 1 4 6 39 -> 1 4 3 69 1 4 3 6 9-> 1 4 3 6 9

  • 8/13/2019 Slides on Data Structures tree and graph

    80/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9 1 6 4 3 9 -> 1 4 63 9 1 4 6 39 -> 1 4 3 69 1 4 3 6 9-> 1 4 3 6 9 Second Pass

  • 8/13/2019 Slides on Data Structures tree and graph

    81/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9 1 6 4 3 9 -> 1 4 63 9 1 4 6 39 -> 1 4 3 69 1 4 3 6 9-> 1 4 3 6 9 Second Pass 43 6 9 -> 43 6 9 *No swap takes place

  • 8/13/2019 Slides on Data Structures tree and graph

    82/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9 1 6 4 3 9 -> 1 4 63 9 1 4 6 39 -> 1 4 3 69 1 4 3 6 9-> 1 4 3 6 9 Second Pass 43 6 9 -> 43 6 9 *No swap takes place 1 4 36 9 -> 1 3 46 9

  • 8/13/2019 Slides on Data Structures tree and graph

    83/94

  • 8/13/2019 Slides on Data Structures tree and graph

    84/94

    Suppose the array to be sorted is : 6 1 4 3 9 First Pass 6 4 3 9 -> 64 3 9 1 6 4 3 9 -> 1 4 63 9

    1 4 6 39 -> 1 4 3 69 1 4 3 6 9-> 1 4 3 6 9 Second Pass 43 6 9 -> 43 6 9 *No swap takes place

    1 4 36 9 -> 1 3 46 9 1 3 4 69 -> 1 3 4 69 1 3 4 6 9-> 1 3 4 6 9

  • 8/13/2019 Slides on Data Structures tree and graph

    85/94

    Third Pass 34 6 9 -> 34 6 9 *No swap takes place 1 3 46 9 -> 1 3 46 9 1 3 4 69 -> 1 3 4 69 1 3 4 6 9-> 1 3 4 6 9

  • 8/13/2019 Slides on Data Structures tree and graph

    86/94

    Given the array: 6 8 1 4 5 3 7 2 - and the goal is toput it into ascending order.

  • 8/13/2019 Slides on Data Structures tree and graph

    87/94

  • 8/13/2019 Slides on Data Structures tree and graph

    88/94

  • 8/13/2019 Slides on Data Structures tree and graph

    89/94

    Given the array: 6 8 1 4 5 3 7 2 - and the goal is toput it into ascending order.

    6 8 1 4 5 3 7 2 (Consider index 0) 6 8 1 4 5 3 7 2 (Consider indices 0 - 1) 6 84 5 3 7 2 (Consider indices 0 - 2)

  • 8/13/2019 Slides on Data Structures tree and graph

    90/94

    Given the array: 6 8 1 4 5 3 7 2 - and the goal is toput it into ascending order.

    6 8 1 4 5 3 7 2 (Consider index 0) 6 8 1 4 5 3 7 2 (Consider indices 0 - 1) 6 84 5 3 7 2 (Consider indices 0 - 2) 4 6 85 3 7 2 (Consider indices 0 -3)

  • 8/13/2019 Slides on Data Structures tree and graph

    91/94

    Given the array: 6 8 1 4 5 3 7 2 - and the goal is toput it into ascending order.

    6 8 1 4 5 3 7 2 (Consider index 0) 6 8 1 4 5 3 7 2 (Consider indices 0 - 1) 6 84 5 3 7 2 (Consider indices 0 - 2) 4 6 85 3 7 2 (Consider indices 0 -3) 4 5 6 83 7 2 (Consider indices 0 -4)

  • 8/13/2019 Slides on Data Structures tree and graph

    92/94

    Given the array: 6 8 1 4 5 3 7 2 - and the goal is toput it into ascending order.

    6 8 1 4 5 3 7 2 (Consider index 0) 6 8 1 4 5 3 7 2 (Consider indices 0 - 1) 6 84 5 3 7 2 (Consider indices 0 - 2) 4 6 85 3 7 2 (Consider indices 0 -3) 4 5 6 83 7 2 (Consider indices 0 -4) 3 4 5 6 7 82 (Consider indices 0 -5)

  • 8/13/2019 Slides on Data Structures tree and graph

    93/94

    Given the array: 6 8 1 4 5 3 7 2 - and the goal is toput it into ascending order.

    6 8 1 4 5 3 7 2 (Consider index 0) 6 8 1 4 5 3 7 2 (Consider indices 0 - 1) 6 84 5 3 7 2 (Consider indices 0 - 2) 4 6 85 3 7 2 (Consider indices 0 -3) 4 5 6 83 7 2 (Consider indices 0 -4) 3 4 5 6 7 82 (Consider indices 0 -5) 2 3 4 5 6 7 8( the array is sorted!)

  • 8/13/2019 Slides on Data Structures tree and graph

    94/94