dastalDEMO

Embed Size (px)

Citation preview

  • 8/8/2019 dastalDEMO

    1/30

    Alexandro B. Ponteras

  • 8/8/2019 dastalDEMO

    2/30

    DATA

    STRUCTURES &ALGORITHMS

    Graphs & Trees

  • 8/8/2019 dastalDEMO

    3/30

    Data Structures:A data structure is a way to store and organize data in order

    to facilitate access and modifications.

    Algorithm

    an algorithm is any well-defined computational procedure that takes

    input and produces output.

    A sequence of computational steps that transform the input into the

    output.

    A tool for solving a well-specified computational problem.

    The algorithm describes a specific computational procedure for

    achieving that input/output relationship.

    e.g.) sorting problem sorting algorithm

  • 8/8/2019 dastalDEMO

    4/30

    .. continued.

    An algorithm is said to be correct if it produce correct

    output for every input instance.

    A correct algorithm solves the given problem.

    An incorrect algorithm might not result at all or

    it might result with an answer other than the desiredone.

  • 8/8/2019 dastalDEMO

    5/30

    Outline

    Graphs Definition

    Applications

    Terminology

    Digraphs

    Undirected Graph

    Weighted graphs

    Trees

    Definition

    Tree traversal

  • 8/8/2019 dastalDEMO

    6/30

    Graph A graph is a pair(V, E), where

    Vis a set of nodes, called vertices Eis a collection of pairs of vertices, called edges

    Vertices and edges are positions and store elements

    Example:

    A vertex represents a town and stores the three-letter towncode

    An edge represents a route between two towns and storesthe mileage of the route

    SAGSC

    BINHIN

    BAC

    BAG

    KAB

    PUL

  • 8/8/2019 dastalDEMO

    7/30

    Edge Types Directed edge

    ordered pair of vertices (u,v) first vertex u is the origin

    second vertex v is thedestination

    e.g., a trip

    Undirected edge unordered pair of vertices

    (u,v)

    e.g., a trip route

    Directed graph

    all the edges are directed e.g., trip network

    Undirected graph

    all the edges are undirected

    e.g., route network

    BAC SCtrip

    AA 1206

    BAC SC849

    miles

  • 8/8/2019 dastalDEMO

    8/30

    John

    DavidPaul

    brown.edu

    cox.net

    cs.brown.edu

    att.net

    qwest.net

    math.brown.edu

    cslab1bcslab1a

    Applications Electronic circuits

    Printed circuit board

    Integrated circuit

    Transportation networks

    Highway network

    Flight network

    Computer networks

    Local area network

    Internet

    Web Databases

    Entity-relationship

    diagram

  • 8/8/2019 dastalDEMO

    9/30

    Terminology End vertices (or endpoints)

    of an edge

    U and V are theendpoints of a

    Edges incident on a vertex

    a, d, and b are incident

    on V Adjacent vertices

    U and V are adjacent

    Degree of a vertex

    X has degree 5 Parallel edges

    h and i are parallel edges

    Self-loop

    j is a self-loop

    XU

    V

    W

    Z

    Y

    a

    c

    b

    e

    d

    f

    g

    h

    i

    j

  • 8/8/2019 dastalDEMO

    10/30

    P1

    Terminology(continued) Path

    sequence of alternatingvertices and edges

    begins with a vertex

    ends with a vertex

    each edge is preceded andfollowed by its endpoints

    Simple path

    path such that all its verticesand edges are distinct

    Examples P1=(V,b,X,h,Z) is a simple

    path

    P2=(U,c,W,e,X,g,Y,f,W,d,V) isa path that is not simple

    XU

    V

    W

    Z

    Y

    a

    c

    b

    e

    d

    f

    g

    hP2

  • 8/8/2019 dastalDEMO

    11/30

    Terminology (cont.) Cycle

    circular sequence of

    alternating vertices and

    edges

    each edge is preceded and

    followed by its endpoints Simple cycle

    cycle such that all its vertices

    and edges are distinct

    Examples

    C1=(V,b,X,g,Y,f,W,c,U,a,) is

    a simple cycle

    C2=(U,c,W,e,X,g,Y,f,W,d,V,a,

    ) is a cycle that is not simple

    C1

    XU

    V

    W

    Z

    Y

    a

    c

    b

    e

    d

    f

    g

    hC2

  • 8/8/2019 dastalDEMO

    12/30

    Digraphs

    A digraph is a graph

    whose edges are all

    directed

    Short for directed graph

    A

    C

    E

    B

    D

  • 8/8/2019 dastalDEMO

    13/30

    Digraph Properties

    A graph G=(V,E) such that

    Each edge goes in one direction:

    Edge (a,b) goes from a to b, but not b to a.

    A

    C

    E

    B

    D

  • 8/8/2019 dastalDEMO

    14/30

    Undirected Graph

    Simply connects two vertices

  • 8/8/2019 dastalDEMO

    15/30

    Weighted Graphs

    In a weighted graph, each edge has an associatednumerical value, called the weight of the edge

    Edge weights may represent, distances, costs, etc.

    Example:

    In a trip route graph, the weight of an edge represents thedistance in miles between the endpoint towns

    SAGSC

    BINHIN

    BAC

    BAG

    KAB

    PUL

  • 8/8/2019 dastalDEMO

    16/30

    Shortest Path Problem

    Given a weighted graph and two vertices u and v, we want to

    find a path of minimum total weight between u and v.

    Length of a path is the sum of the weights of its edges.

    Example:

    Shortest path between Pulupandan and San Carlos

    SAGSC

    BINHIN

    BAC

    BAG

    KAB

    PUL

  • 8/8/2019 dastalDEMO

    17/30

    Tree

    A tree is a non-empty set, one element of

    which is designated the root of the tree while

    the remaining elements are partitioned into

    non-empty sets each of which is a sub tree ofthe root.

  • 8/8/2019 dastalDEMO

    18/30

    Example of a Tree

  • 8/8/2019 dastalDEMO

    19/30

    Tree Traversal

    tree-traversal refers to the process of visiting

    each node in a tree data structure, exactly

    once, in a systematic way. Such traversals

    are classified by the order in which the nodesare visited.

  • 8/8/2019 dastalDEMO

    20/30

    Tree Traversal Order

    preorder

    inorder

    postorder

  • 8/8/2019 dastalDEMO

    21/30

    Preorder traversal

    Visit the root.

    Traverse the left subtree.

    Traverse the right subtree.

  • 8/8/2019 dastalDEMO

    22/30

  • 8/8/2019 dastalDEMO

    23/30

    Preorder traversal sequence

    F, B, A, D, C, E,G, I, H

  • 8/8/2019 dastalDEMO

    24/30

    Inorder traversal

    Traverse the left subtree.

    Visit the root.

    Traverse the right subtree.

  • 8/8/2019 dastalDEMO

    25/30

  • 8/8/2019 dastalDEMO

    26/30

    Inorder traversal sequence

    A, B, C, D, E, F,G, H, I

  • 8/8/2019 dastalDEMO

    27/30

    Postorder traversal

    Traverse the left subtree.

    Traverse the right subtree.

    Visit the root.

  • 8/8/2019 dastalDEMO

    28/30

  • 8/8/2019 dastalDEMO

    29/30

    Postorder traversal sequence

    A, C, E, D, B, H, I,G, F

  • 8/8/2019 dastalDEMO

    30/30

    END