trees and graphs

183
Data Structure and Algorithm Analysis Slide Set 6 Trees & Graph 13 CS- I & II Engr. Maria Shaikh [email protected]

Upload: maya

Post on 16-Dec-2015

226 views

Category:

Documents


0 download

DESCRIPTION

DATA STRUCTURES AND ALGORITHM ANALYSIS

TRANSCRIPT

  • Data Structure and Algorithm AnalysisSlide Set 6

    Trees & Graph

    13 CS- I & II

    Engr. Maria Shaikh

    [email protected]

  • Non-Linear Data Structure

    These data structures do not have their elements in a sequence. Trees and

    Graph is an example.

    Trees are mainly used to represent data containing a hierarchical relationship

    between elements, example : records, family trees and table of contents.

    10/30/2014 Engr. Maria Shaikh 2

  • Non Linear Data Structures

    Non Linear Data Structures are

    Branched recursive data structures

    Consisting of nodes

    Each node can be connected to other nodes

    Examples of tree-like structures

    Trees: binary / other degree, balanced, etc.

    Graphs: directed / undirected, weighted, etc.

    Networks

    3

  • 4Project Manager

    Team Leader

    De-signer

    QA Team Leader

    Developer 1

    Developer 2

    Tester 1Developer

    3 Tester 2

    Tree

    Graph

    2

    36

    1

    45

    7

    19

    21

    141

    12 31

    4

    11

    Network2

    36

    1

    45

    2

    36

    1

    45

  • Basic Terminologies in Trees

    Nodes: The boxes on the tree are called nodes.

    Children: The nodes immediately below (to the left and right of) a given node

    are called its children.

    Parent: The node immediately above a given node is called its parent.

    Root: The (unique) node without a parent is called the root.

    Leaf: A node with no children is called a leaf

    Siblings: Two children of the same parent are said to be siblings.

    A node can be an ancestor (e.g.grandparent) or a descendant (e.g. great-

    grandchild).

    10/30/2014 Engr. Maria Shaikh 5

  • Basic Terminologies in Trees

    Root: node with no parent.

    A non-empty tree has exactly one root.

    leaf: node with no children

    siblings: two nodes with the same parent.

    10/30/2014 Engr. Maria Shaikh 6

  • Basic Terminologies in Graph

    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.

    10/30/2014 Engr. Maria Shaikh 7

  • Trees

    Terminology

    Root no parent

    Leaf no child

    Interior non-leaf

    Height distance from root to leaf

    Root node

    Leaf nodes

    Interior nodes Height

  • Trees Data Structures

    Tree

    Nodes

    Each node can have 0 or more children

    A node can have at most one parent

    Binary tree

    Tree with 02 children per node

    Tree

    Binary Tree

  • 10

    Size and depth

    The size of a binary tree is the number of nodes

    in it

    This tree has size 12

    The depth of a node is its distance from the root.

    a is at depth zero

    e is at depth 2

    The depth of a binary tree is the depth of its

    deepest node

    This tree has depth 4

    a

    b c

    d e f

    g h i j k

    l

  • Trees

    Data structure terminology

    Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth,

    height, subtree

    Height = 2

    Depth 0

    Depth 1

    Depth 2

    17

    15149

    6 5 8

    11

  • Binary Trees

    A binary tree is a finite set of elements that are either empty or ispartitioned into three disjoint subsets. The first subset contains asingle element called the root of the tree. The other two subsets arethemselves binary trees called the left and right subtrees of theoriginal tree. A left or right subtree can be empty.

    Each element of a binary tree is called a node of the tree.

    10/30/2014 Engr. Maria Shaikh 12

  • Binary Trees

    Binary trees: most widespread form

    Each node has at most 2 children

    10

    17

    159

    6 5 8

    Root

    nodeLeft

    subtree

    Right

    Subtree

    Left child

    13

    Right child

    Engr. Maria Shaikh

  • Complete Binary Tree

    A complete binary tree is a binary tree in which every level,

    except possibly the last, is completely filled, and all nodes are as

    far left as possible.

    10/30/2014 Engr. Maria Shaikh 14

  • Full and Complete Binary Tree

    15

  • BINARY TREES: BASIC DEFINITIONS

    leaves

    left son right son

    Engr. Maria Shaikh

  • Binary Search Trees

    Binary search trees are ordered

    For each node x in the tree

    All the elements of the left subtree of x are < x

    All the elements of the right subtree of x are x

    17 Engr. Maria Shaikh

  • Binary Search Trees

    Engr. Maria Shaikh

  • All items in the left subtree are less than the root.

    All items in the right subtree are greater or equal to the root.

    Each subtree is itself a binary search tree.

    Binary search trees provide an excellent structure for searching a list

    and at the same time for inserting and deleting data into the list.

    A Binary Search Tree is a binary tree with the following properties:

    Engr. Maria Shaikh

    Binary Search Trees

  • Balanced Binary Tree

    A binary tree is balanced if every level above the lowest is full (contains 2n nodes)

    In most applications, a reasonably balanced binary tree is desirable.

    a

    b c

    d e f g

    h i j

    A balanced binary tree

    a

    b

    c

    d

    e

    f

    g h

    i jAn unbalanced binary tree

    Engr. Maria Shaikh

  • (a), (b) - complete and balanced trees;

    (d) nearly complete and balanced tree;

    (c), (e) neither complete nor balanced trees

    Engr. Maria Shaikh

  • Engr. Maria Shaikh

    Invalid Binary Search Tree

  • Binary Search Tree Operations

    We discuss four basic BST operations: traversal, search, insert, and delete; and

    develop algorithms for searches, insertion, and deletion.

    Traversals

    Searches

    Insertion

    Deletion

    Engr. Maria Shaikh

  • TRAVERSING BINARY TREES

    One of the common operations of a binary tree is to traverse the tree. Traversinga tree is to pass through all of its nodes once. You may want to print thecontents of each node or to process the contents of the nodes. In either case eachnode of the tree is visited.

    There are three main traversal methods where traversing a binary tree involvesvisiting the root and traversing its left and right subtrees. The only differenceamong these three methods is the order in which these three operations areperformed.

  • Binary Tree Traversal

    Engr. Maria Shaikh

  • Engr. Maria Shaikh

  • TRAVERSING BINARY TREES

    Traversing a binary tree in preorder (depth-first order)

    1. Visit the root.

    2. Traverse the left subtree in preorder.

    3. Traverse the right subtree in preorder.

  • Traversing a binary tree in preorder

    Preorder: ABDGCEHIF

  • Preorder Traversal

    23 18 12 20 44 35 52

    Engr. Maria Shaikh

  • Postorder Traversal

    Traversing a binary tree in postorder

    1. Traverse the left subtree in postorder.

    2. Traverse the right subtree in postorder.

    3. Visit the root.

  • Postorder Traversal

    12 20 18 35 52 44 23

    Engr. Maria Shaikh

  • Postorder Traversal

    Postorder: GDBHIEFCA

    Engr. Maria Shaikh

  • Inorder Traversing Binary Trees

    Traversing a binary tree in inorder (or symmetric order)

    1. Traverse the left subtree in inorder.

    2. Visit the root.

    3. Traverse the right subtree in inorder.

  • Inorder Traversal

    12 18 20 23 35 44 52

    Inorder traversal of a binary search tree produces a sequenced list

    Engr. Maria Shaikh

  • Inorder Traversal

    Inorder: DGBAHEICF

  • Right-Node-Left Traversal

    52 44 35 23 20 18 12

    Right-node-left traversal of a binary search tree produces a descending sequence

    Engr. Maria Shaikh

  • Binary Expression Trees

    10/30/2014 Engr. Maria Shaikh 37

    *

    +

    b

    -a

    c

    d

    --

    /

    e + h

    gf

    [a + (b c)] * [(d e) / (f + g h)]

    Preorder: * + a b c / - d e - + f g h

    Postorder: a b c - + d e f g + h - / *

  • Tree Traversal Algorithms

    Traversing a tree means to visit each of its nodes exactly one in particular order

    Many traversal algorithms are known

    Depth-First Search (DFS)

    Visit node's successors first

    Usually implemented by recursion

    Breadth-First Search (BFS)

    Nearest nodes visited first

    Implemented by a queue38

  • Depth-First Search first visits all descendants of given node recursively, finally visits the node itself

    DFS algorithm pseudo code

    Depth-First Search (DFS)

    DFS(node)

    {

    for each child c of node

    DFS(c);

    print the current node;

    }

    7

    1419

    23 6

    21

    311 121 2 3

    4 5 8

    6 7

    9

    39

  • DFS in Action (Step 1)

    Stack: 7

    Output: (empty)

    40

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 2)

    Stack: 7, 19

    Output: (empty)

    41

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 3)

    Stack: 7, 19, 1

    Output: (empty)

    42

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 4)

    Stack: 7, 19

    Output: 1

    43

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 5)

    Stack: 7, 19, 12

    Output: 1

    44

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 6)

    Stack: 7, 19

    Output: 1, 12

    45

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 7)

    Stack: 7, 19, 31

    Output: 1, 12

    46

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 8)

    Stack: 7, 19

    Output: 1, 12, 31

    47

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 9)

    Stack: 7

    Output: 1, 12, 31, 19

    48

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 10)

    Stack: 7, 21

    Output: 1, 12, 31, 19

    49

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 11)

    Stack: 7

    Output: 1, 12, 31, 19, 21

    50

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 12)

    Stack: 7, 14

    Output: 1, 12, 31, 19, 21

    51

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 13)

    Stack: 7, 14, 23

    Output: 1, 12, 31, 19, 21

    52

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 14)

    Stack: 7, 14

    Output: 1, 12, 31, 19, 21, 23

    53

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 15)

    Stack: 7, 14, 6

    Output: 1, 12, 31, 19, 21, 23

    54

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 16)

    Stack: 7, 14

    Output: 1, 12, 31, 19, 21, 23, 6

    55

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 17)

    Stack: 7

    Output: 1, 12, 31, 19, 21, 23, 6, 14

    56

    7

    1419

    23 6

    21

    311 12

  • DFS in Action (Step 18)

    Stack: (empty)

    Output: 1, 12, 31, 19, 21, 23, 6, 14, 7

    57

    7

    1419

    23 6

    21

    311 12

    Traversal finished

  • Breadth-First Search first visits the neighbor nodes, later their neighbors, etc.

    BFS algorithm pseudo code

    Breadth-First Search (BFS)

    BFS(node)

    {

    queue node

    while queue not empty

    v queue

    print v

    for each child c of v

    queue c

    }

    7

    1419

    23 6

    21

    311 125 6 7

    2 3 4

    8 9

    1

    58

  • BFS in Action (Step 1)

    Queue: 7

    Output: 7

    59

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 2)

    Queue: 7, 19

    Output: 7

    60

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 3)

    Queue: 7, 19, 21

    Output: 7

    61

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 4)

    Queue: 7, 19, 21, 14

    Output: 7

    62

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 5)

    Queue: 7, 19, 21, 14

    Output: 7, 19

    63

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 6)

    Queue: 7, 19, 21, 14, 1

    Output: 7, 19

    64

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 7)

    Queue: 7, 19, 21, 14, 1, 12

    Output: 7, 19

    65

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 8)

    Queue: 7, 19, 21, 14, 1, 12, 31

    Output: 7, 19

    66

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 9)

    Queue: 7, 19, 21, 14, 1, 12, 31

    Output: 7, 19, 21

    67

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 10)

    Queue: 7, 19, 21, 14, 1, 12, 31

    Output: 7, 19, 21, 14

    68

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 11)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23

    Output: 7, 19, 21, 14

    69

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 12)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14

    70

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 13)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14, 1

    71

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 14)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14, 1, 12

    72

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 15)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14, 1, 12, 31

    73

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 16)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14, 1, 12, 31, 23

    74

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 16)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14, 1, 12, 31, 23, 6

    75

    7

    1419

    23 6

    21

    311 12

  • BFS in Action (Step 17)

    Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

    Output: 7, 19, 21, 14, 1, 12, 31, 23, 6

    76

    7

    1419

    23 6

    21

    311 12

    The queue is

    empty stop

  • General Trees

    A general tree ( sometimes called a tree) is defined to be a non empty finite set T of elements, called nodes, such that:

    1) T contains a distinguished element R, called the root of T.

    2) The remaining elements of T form an ordered collection of zero or more disjoint trees

    T1, T2, -------------TM are called successors of R.

    Engr. Maria Shaikh

  • General Trees

    10/30/2014 Engr. Maria Shaikh 78

    A

    B

    FE

    HGK

    D

    J

    NMGeneral Tree T with 13 Nodes

    A, B, C, D, E, F, G, H, J, K, L, M, N

    C

    L

  • Presentation topic

    GRAPH

    Engr. Maria Shaikh

  • What is graph ?

    A GRAPH is a mathematical structure consisting of a set of vertices and a set

    of edges.

    Node: Each element of a graph is called node of a graph.

    Edge :Line joining two nodes is called an edge.

    = Vertices

    = Edeges

    Engr. Maria Shaikh

  • Other Definitions of Graph

    A data structure that consists of a set of nodes (vertices) and a set of

    edges that relate the nodes to each other.

    The set of edges describes relationships among the vertices.

    Graph is a mathematical structure used to model pair wise

    relations between objects from a certain collection.

    10/30/2014 81Engr. Maria Shaikh

  • Formal definition of graphs

    A graph G is defined as follows:

    G = (V,E) is composed of:

    V(G): a finite, nonempty set of vertices

    E(G): a set of edges connecting the vertices in V

    An edge e = (u,v) is a pair of vertices

    Example: a b

    c

    d e

    V= {a,b,c,d,e}

    E= {(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),

    (d,e) }

    Engr. Maria Shaikh

  • Types of graphs

    There are two types of graph :

    Directed graph

    Undirected graph

    Engr. Maria Shaikh

  • Directed graph

    A graphs G is called directed graph or digraph if each edge has a direction.

    Engr. Maria Shaikh

  • Un Directed graph

    A graphs G is called directed graph if each edge has no direction.

    Engr. Maria Shaikh

  • Graph terminologies

    Adjacent nodes: Two nodes are adjacent if they are connected by an edge

    Path: A sequence of vertices that connect two nodes in a graph

    Complete graph: A graph in which every vertex is directly connected to every other vertex.

    5 is adjacent to 7

    7 is adjacent from 55

    Engr. Maria Shaikh

  • What is the number of edges in a complete directed graph with N vertices?

    N * (N-1)

    Graph terminologies (cont.)

    2( )O N

    Engr. Maria Shaikh

  • What is the number of edges in a complete undirected graph with N vertices?

    N * (N-1) / 2

    Graph terminologies (cont.)

    2( )O N

    Engr. Maria Shaikh

  • Terminologies

    subgraph: subset of vertices and edges forming a graph

    connected component: maximal connected subgraph. E.g., the graph below has 3 connected components.

    connected not connected

    connected graph: any two vertices are connected by some path

    Engr. Maria Shaikh

  • 0 0

    1 2 3

    1 2 0

    1 2

    3(i) (ii) (iii) (iv)(a) Some of the subgraph of G1

    0 0

    1

    0

    1

    2

    0

    1

    2

    (i) (ii) (iii) (iv)

    (b) Some of the subgraph of G3

    0

    1 2

    3

    G1

    0

    1

    2G3

    Subgraphs Examples

  • Some different types of graph

    1. Sub graph

    2. Complete graph

    Engr. Maria Shaikh

  • 3. Weighted graph:

    A graph in which each edge carries a value.

    Some different types of graph

    Engr. Maria Shaikh

  • Representation of graphs

    Graphs can be represented in 2 ways :

    Array representation

    Engr. Maria Shaikh

  • Linked list representation

    Engr. Maria Shaikh

  • Engr. Maria Shaikh

    Real Life Example

  • Engr. Maria Shaikh

    Real Life Example

  • Engr. Maria Shaikh

    Real Life Example

  • Real Life Example

    electronic circuits networks (roads, flights, communications)

    CS16

    LAX

    JFK

    LAX

    DFW

    STL

    HNL

    FTLEngr. Maria Shaikh

  • Paths

    A path in a graph is a sequence of vertices such that from

    each of its vertices there is an edge to the next vertex in the

    sequence.

    The length of a path is the number of edges on it. The length can be zero for the case of a single vertex.

    Engr. Maria Shaikh

  • A path may be infinite.

    A finite path always has a first vertex, called its start vertex, and a last vertex, called its end vertex.

    Both of them are called terminal vertices of the path.

    The other vertices in the path are internal vertices.

    Path= A B D C E

    C A- Start vertex

    E- End vertex

    AB

    D

    E

    Paths

    Engr. Maria Shaikh

  • Path

    101

    3

    3 3

    3

    2

    a b

    c

    d e

    a b

    c

    d e

    a b e d c b e d c

    Engr. Maria Shaikh

  • A graph with no loops or multiple edges is called a simple graph.

    A path with no repeated vertices is called a simple path.

    The path from v1 to v4 is said to be simple path as

    vertices is touched more than once.

    The path from v1 to v4 is not simple as

    v1 is touched twice or looped.

    cycle: Simple path, except that the last vertex is the same as

    the first vertex. Its also known as a circuit or circular path.

    Path= A E C A

    A B

    C D

    E

    Simple Paths

    Engr. Maria Shaikh

  • Paths

    simple path

    cycle:

    a b

    c

    d e

    b e c

    a c d a

    a b

    c

    d e

    Engr. Maria Shaikh

  • Degree The degree of vertex in an undirected graph is the number of edges incident to

    that vertex.

    A vertex with degree one is called pendent vertex or end vertex.

    A vertex with degree zero and hence has no incident edges is called an isolated vertex.

    In the undirected graph vertex v3 has the degree 3 And vertex v2 has the degree 2

    V1

    B

    A

    Pendent vertexIsolated vertex

    Engr. Maria Shaikh

  • Degree in directed graph

    Degree of directed graph has two types

    i. Indegree

    No of edges with their head towards the vertex.

    ii. Outdegree

    No of edges with their tail towards the vertex.

    Indegree of vertex v2 is 2 and

    Outdegree of vertex v1 is 1.

    Degree

    Engr. Maria Shaikh

  • 01 2

    3 4 5 6

    G

    G2

    3 2

    3 3

    1 1 1 1

    Directed graph

    in-degree

    out-degree

    0

    1

    2

    G3

    in:1, out: 1

    in: 1, out: 2

    in: 1, out: 0

    0

    1 2

    3

    33

    3

    Example:

    Engr. Maria Shaikh

  • Adjacent and Incident

    If (v0, v1) is an edge in an undirected graph,

    v0 and v1 are adjacent

    The edge (v0, v1) is incident on vertices v0 and v1

    If is an edge in a directed graph

    v0 is adjacent to v1, and v1 is adjacent from v0

    The edge is incident on v0 and v1

    Engr. Maria Shaikh

  • More

    Tree - connected graph without cycles.

    Forest - collection of trees.

    A forest is an undirected graph

    without cycles

    The connected components of a

    forest are trees.

    tree

    forest

    tree

    tree

    tree

    Forest

    Engr. Maria Shaikh

  • 109

    Cycles and trees

    Graph with no cycles: acyclic

    Directed Acyclic Graph: DAG

    Undirected forest:

    Acyclic undirected graph

    Tree: undirected acyclic connected graph

    one connected component

    Engr. Maria Shaikh

  • Connectivity

    Let n = #vertices, and m = #edges

    A complete graph: one in which all pairs of vertices are adjacent

    How many total edges in a complete graph?

    Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice!

    Therefore, intuitively, m = n(n -1)/2.

    Therefore, if a graph is not complete, m < n(n -1)/2

    n 5m (5

    Engr. Maria Shaikh

  • More Connectivity

    n = #vertices

    m = #edges

    For a tree m = n - 1

    n 5m 4

    n 5m 3

    If m < n - 1, G is

    not connected

    Engr. Maria Shaikh

  • Graph Representations

    Adjacency Matrix

    Adjacency Lists

    Engr. Maria Shaikh

  • 113

    Representing graphs

    Adjacency matrix:

    When graph is dense

    |E| close to |V|2

    Adjacency lists:

    When graph is sparse

    |E|

  • Adjacency Matrix

    Let G=(V,E) be a graph with n vertices.

    The adjacency matrix of G is a two-dimensional n by n array, say adj_mat

    If the edge (vi, vj) is in E(G), adj_mat[i][j]=1

    If there is no such edge in E(G), adj_mat[i][j]=0

    The adjacency matrix for an undirected graph is symmetric; the adjacency

    matrix for a digraph need not be symmetric

    Engr. Maria Shaikh

  • 115

    Examples

    1 2 3

    1 0 0 1

    2 0 1 0

    3 1 1 0

    1

    3

    2

    32

    1

    4

    1 2 3 4

    1 0 1 1 0

    2 1 0 0 0

    3 1 0 0 0

    4 0 0 0 0Engr. Maria Shaikh

  • Adjacency Matrix

    |V| |V| matrix A.

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

    A is then given by:

    otherwise0

    ),( if1],[

    EjiajiA ij

    a

    dc

    b1 2

    3 4

    1 2 3 4

    1 0 1 1 1

    2 0 0 1 0

    3 0 0 0 1

    4 0 0 0 0

    a

    dc

    b1 2

    3 4

    1 2 3 4

    1 0 1 1 1

    2 1 0 1 0

    3 1 1 0 1

    4 1 0 1 0

    A = AT for undirected graphs.

    Engr. Maria Shaikh

  • Examples for Adjacency Matrix

    0

    1

    1

    1

    1

    0

    1

    1

    1

    1

    0

    1

    1

    1

    1

    0 0

    1

    0

    1

    0

    0

    0

    1

    0

    0

    1

    1

    0

    0

    0

    0

    0

    1

    0

    0

    1

    0

    0

    0

    0

    1

    0

    0

    1

    0

    0

    0

    0

    0

    1

    1

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    1

    0

    0

    0

    0

    0

    0

    1

    0

    1

    0

    0

    0

    0

    0

    0

    1

    0

    1

    0

    0

    0

    0

    0

    0

    1

    0

    G1

    G2

    G4

    0

    1 2

    3

    0

    1

    2

    1

    0

    2

    3

    4

    5

    6

    7

    symmetricundirected: n2/2

    directed: n2

    Engr. Maria Shaikh

  • Merits of Adjacency Matrix

    From the adjacency matrix, to determine the connection of vertices is

    easy

    The degree of a vertex is

    For a digraph (= directed graph), the row sum is the out_degree, while

    the column sum is the in_degree

    adj mat i jj

    n

    _ [ ][ ]

    0

    1

    ind vi A j ij

    n

    ( ) [ , ]

    0

    1

    outd vi A i jj

    n

    ( ) [ , ]

    0

    1

    Engr. Maria Shaikh

  • 01

    2

    3

    0

    1

    2

    0

    1

    2

    3

    4

    5

    6

    7

    1 2 3

    0 2 3

    0 1 3

    0 1 2

    G1

    1

    0 2

    G3

    1 2

    0 3

    0 3

    1 2

    5

    4 6

    5 7

    6

    G4

    0

    1 2

    3

    0

    1

    2

    1

    0

    2

    3

    4

    5

    6

    7

    An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

    Adjacency Lists(data structures)

  • 120

    Traversing a Graph

    A traversal of a graph is a systematic procedure for exploring a

    graph by examining all of its vertices and edges.

    Until finding a goal vertex or until no more vertices

    Only for connected graphs

    Example:

    Web spider or crawler is the data collecting part of a search engine that

    visits all the hypertext documents on the web. (The documents are

    vertices and the hyperlinks are the edges).

    Engr. Maria Shaikh

  • 1.121

    Graph Search (traversal)

    How do we search a graph?

    At a particular vertices, where shall we go next?

    Two common framework:

    the depth-first search (DFS)

    the breadth-first search (BFS) and

    In DFS, go as far as possible along a single path until reach a dead

    end (a vertex with no edge out or no neighbor unexplored) then

    backtrack

    In BFS, one explore a graph level by level away (explore all

    neighbors first and then move on)

    Engr. Maria Shaikh

  • 122

    Breadth-first search

    Level-by-level traversal.

    Search for all vertices that are directly reachable from the root (called

    level 1 vertices)

    After mark all these vertices, visit all vertices that are directly reachable

    from any level 1 vertices (called level 2 vertices), and so on.

    In general, level k vertices are directly reachable from a level k 1

    vertices

    Engr. Maria Shaikh

  • 123

    BFS in a binary tree

    BFS: visit all siblings before their descendants

    5

    2

    1 3

    8

    6 10

    7 95 2 8 1 3 6 10 7 9

    Engr. Maria Shaikh

  • 124

    BFS for General Graphs

    This version assumes vertices have two children

    left, right

    This is trivial to fix

    But still no good for general graphs

    It does not handle cycles

    Engr. Maria Shaikh

  • 125

    Start with A. Put in the queue (marked red)

    A

    B

    G C

    E

    D

    F

    Queue: A

    Example for Undirected Graph

    Engr. Maria Shaikh

  • 126

    B and E are next

    A

    B

    G C

    E

    D

    F

    Queue: A B E

    Example for Undirected Graph (cont.)

    Engr. Maria Shaikh

  • 127

    When we go to B, we put G and C in the queue

    When we go to E, we put D and F in the queue

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for Undirected Graph (cont.)

    Engr. Maria Shaikh

  • 128

    When we go to B, we put G and C in the queue

    When we go to E, we put D and F in the queue

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for Undirected Graph (cont.)

    Engr. Maria Shaikh

  • 129

    Suppose we now want to expand C. We put F in the queue again!

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F F

    Example for Undirected Graph (cont.)

    Engr. Maria Shaikh

  • 130

    Generalizing BFS

    Cycles:

    We need to save auxiliary information

    Each node needs to be marked

    Visited: No need to be put on queue

    Not visited: Put on queue when found

    What about assuming only two children vertices?

    Need to put all adjacent vertices in queue

    Engr. Maria Shaikh

  • 131

    The general BFS algorithm

    Each vertex can be in one of three states:

    Unmarked and not on queue

    Marked and on queue

    Marked and off queue

    The algorithm moves vertices between these states

    Engr. Maria Shaikh

  • 132

    Handling vertices

    Unmarked and not on queue:

    Not reached yet

    Marked and on queue:

    Known, but adjacent vertices not visited yet (possibly)

    Marked and off queue:

    Known, all adjacent vertices on queue or done with

    Engr. Maria Shaikh

  • 133

    Start with A. Mark it.

    A

    B

    G C

    E

    D

    F

    Queue: A

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 134

    Expand As adjacent vertices.

    Mark them and put them in queue.

    A

    B

    G C

    E

    D

    F

    Queue: A B E

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 135

    Now take B off queue, and queue its neighbors.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 136

    Do same with E.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 137

    Visit C.

    Its neighbor F is already marked, so not queued.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 138

    Visit G.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 139

    Visit D. F, E marked so not queued.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 140

    Visit F.

    E, D, C marked, so not queued again.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for Undirected Graph Modified

    Engr. Maria Shaikh

  • 141

    Done. We have explored the graph in order:

    A B E C G D F.

    A

    B

    G C

    E

    D

    F

    Queue: A B E C G D F

    Example for BFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 1.142

    BFS for Directed GraphThe Color Scheme

    White vertices have not been discovered All vertices start out white

    Grey vertices are discovered but not fully explored They may be adjacent to white vertices

    Black vertices are discovered and fully explored They are adjacent only to black and gray vertices

    Explore vertices by scanning adjacency list of grey vertices

    Engr. Maria Shaikh

  • 1.143Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    An Example

  • 1.144Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

  • 1.145Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

  • 1.146Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

    2

    2

  • 1.147Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

    2

    2

    3 3

    3

    3

    3

  • 1.148Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

    2

    2

    3 3

    3

    3

    3

    4 4

    4

  • 1.149Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

    2

    2

    3 3

    3

    3

    3

    4 4

    4

    55

  • 1.150Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

    2

    2

    3 3

    3

    3

    3

    4 4

    4

    55

  • 1.151Data Structure and Algorithm

    A

    ONM

    LKJ

    E F G H

    DCB

    I

    P

    0

    1 1

    1

    2

    2

    3 3

    3

    3

    3

    4 4

    4

    55

  • 152

    Interesting features of BFS

    Complexity: O(|V| + |E|)

    All vertices put on queue exactly once

    For each vertex on queue, we expand its edges

    In other words, we traverse all edges once

    BFS finds shortest path from s to each vertex

    Shortest in terms of number of edges

    Why does this work?

    Engr. Maria Shaikh

  • 1.153

    Depth-First Search (DFS)

    The basic idea behind this algorithm is that it traverses the graph using

    recursion

    Go as far as possible until you reach a dead end

    Backtrack to the previous path and try the next branch

    The graph below, started at node a, would be visited in the following

    order: a, b, c, g, h, i, e, d, f, j

    da b

    h i

    c

    g

    e

    j

    f

  • 154

    Start with A. Mark it.

    A

    B

    G C

    E

    D

    F

    Current vertex: A

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 155

    Expand As adjacent vertices. Pick one (B).

    Mark it and re-visit.

    A

    B

    G C

    E

    D

    F

    Current: B

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 156

    Now expand B, and visit its neighbor, C.

    A

    B

    G C

    E

    D

    F

    Current: C

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 157

    Visit F.

    Pick one of its neighbors, E.

    A

    B

    G C

    E

    D

    F

    Current: F

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 158

    Es adjacent vertices are A, D and F.

    A and F are marked, so pick D.

    A

    B

    G C

    E

    D

    F

    Current: E

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 159

    Visit D. No new vertices available. Backtrack to E. Backtrack to F.

    Backtrack to C. Backtrack to B

    A

    B

    G C

    E

    D

    F

    Current: D

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 160

    Visit G. No new vertices from here. Backtrack to B. Backtrack to A. E

    already marked so no new.

    A

    B

    G C

    E

    D

    F

    Current: G

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 161

    Done. We have explored the graph in order:

    A B C F E D G

    A

    B

    G C

    E

    D

    F

    Current:1

    2

    3

    4

    6

    7

    5

    Example for DFS Undirected Graph Modified

    Engr. Maria Shaikh

  • 1.162

    DFS: Color Scheme

    Vertices initially colored white

    Then colored gray when discovered

    Then black when finished

    Engr. Maria Shaikh

  • 1.163

    DFS: Time Stamps

    Discover time d[u]: when u is first discovered

    Finish time f[u]: when backtrack from u

    d[u] < f[u]

    Engr. Maria Shaikh

  • 1.164Data Structure and Algorithm

    DFS Example

    source

    vertex

    Engr. Maria Shaikh

  • 1.165Data Structure and Algorithm

    DFS Example

    1 | | |

    | | |

    | |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.166Data Structure and Algorithm

    DFS Example

    1 | | |

    | | |

    2 | |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.167Data Structure and Algorithm

    DFS Example

    1 | | |

    | | 3 |

    2 | |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.168Data Structure and Algorithm

    DFS Example

    1 | | |

    | | 3 | 4

    2 | |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.169Data Structure and Algorithm

    DFS Example

    1 | | |

    | 5 | 3 | 4

    2 | |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.170Data Structure and Algorithm

    DFS Example

    1 | | |

    | 5 | 63 | 4

    2 | |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.171Data Structure and Algorithm

    DFS Example

    1 | | |

    | 5 | 63 | 4

    2 | 7 |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.172Data Structure and Algorithm

    DFS Example

    1 | 8 | |

    | 5 | 63 | 4

    2 | 7 |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.173Data Structure and Algorithm

    DFS Example

    1 | 8 | |

    | 5 | 63 | 4

    2 | 7 9 |

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.174Data Structure and Algorithm

    DFS Example

    1 | 8 | |

    | 5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.175Data Structure and Algorithm

    DFS Example

    1 | 8 |11 |

    | 5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.176Data Structure and Algorithm

    DFS Example

    1 |12 8 |11 |

    | 5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.177Data Structure and Algorithm

    DFS Example

    1 |12 8 |11 13|

    | 5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.178Data Structure and Algorithm

    DFS Example

    1 |12 8 |11 13|

    14| 5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.179Data Structure and Algorithm

    DFS Example

    1 |12 8 |11 13|

    14|155 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 1.180Data Structure and Algorithm

    DFS Example

    1 |12 8 |11 13|16

    14|155 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Engr. Maria Shaikh

  • 181

    Interesting features of DFS

    Complexity: O(|V| + |E|)

    All vertices visited once, then marked

    For each vertex on queue, we examine all edges

    In other words, we traverse all edges once

    DFS does not necessarily find shortest path

    Why?

    Engr. Maria Shaikh

  • Graph Traversal

    Problem: Search for a certain node or traverse all nodes in the

    graph

    Depth First Search

    Once a possible path is found, continue the search until the end of the

    path

    Breadth First Search

    Start several paths at a time, and advance in each one step at a time

    Engr. Maria Shaikh

  • END OF SLIDE SET 6

    10/30/2014 Engr. Maria Shaikh 183