DataStructure - Graphs

Embed Size (px)

Citation preview

  • 8/12/2019 DataStructure - Graphs

    1/24

    1

    Lecture outlines

    Definition of Graphs and Related Concepts

    Representation of Graphs Graph Applications

    Graph Traversal

  • 8/12/2019 DataStructure - Graphs

    2/24

    2

    Definition of Graphs

    Graphs are non liner data structures.

    A graph is a finite set of nodes with line between

    nodes

    Nodes are called vertices or points.

    Lines are called edges or arcs.

    Formally, a graph G is a structure (V,E) consisting of

    a finite set V called the set of nodes, and a finite set E called the set of edges/links , of the form

    (x,y) where x and y are nodes in V

  • 8/12/2019 DataStructure - Graphs

    3/24

    CS 103 3

    Application of Graphs

    Graph application area:

    Computer science

    Electrical engineering

    Chemistry

    Data base

    Logic and design

    computer networks

    airline flights

    road map

    course prerequisite structure

    tasks for completing a job

    flow of control through a program

    many more

  • 8/12/2019 DataStructure - Graphs

    4/24

    4

    Graphs Applications

    Electronic circuits

    Networks

    (roads, flights, communications)

    CS16

    LAX

    JFK

    LAX

    DFW

    STL

    FTL

  • 8/12/2019 DataStructure - Graphs

    5/24

    5

    Examples of Graphs

    V={0,1,2,3,4}

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

    01

    4

    2

    3

    When (x,y) is an edge,

    we say that x is adjacent to

    y, and y is adjacent fromx.

    0 is adjacent to 1.

    1 is not adjacent to 0.

    2 is adjacent from 1.

  • 8/12/2019 DataStructure - Graphs

    6/24

    6

    Intuition Behind Graphs

    The nodes represent entities (such as people, cities,computers, words, etc.)

    Edges (x,y) represent relationships between entities x and

    y, such as:

    x is a friend of y (note that this not necessarily reciprocal)

    x is a child of y

  • 8/12/2019 DataStructure - Graphs

    7/24

    7

    Definition and Terminology

    What is a graph?

    A graph G = (V,E) is composed of:

    V: set of vertices

    E: set ofedgesconnecting the verticesin VAn edgee = (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)}

    a b

    c

    V={a, b, c}

    E={,,

    }

  • 8/12/2019 DataStructure - Graphs

    8/24

    8

    Definition and Terminology Graph may be either directedor undirected

    Directed graph (or Digraph)

    Each line has a directionto its successor.

    The lines in a directed graph are known as arcs

    Note

    Undirected graph Each line has nodirection.

    Note =

  • 8/12/2019 DataStructure - Graphs

    9/24

    9

    Definition and Terminology0

    1 2

    3

    0

    1

    2

    0

    1 2

    3 4 5 6G1

    G2G3

    V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}

    V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}V(G3)={0,1,2} E(G3)={,,}

    A graph is said to b a complete graph if there is an edge

    between every pair of vertices.

    complete graph incomplete graph

  • 8/12/2019 DataStructure - Graphs

    10/24

    10

    Loop edge and multiple edges

    0

    1

    (a)

    2

    1

    0

    3

    (b)self edge

    2

    multigraph:

    multiple occurrences

    of the same edge

  • 8/12/2019 DataStructure - Graphs

    11/24

    11

    Definition and Terminology Path: sequence of vertices v1,v2,. . .vk such that consecutive

    vertices viand vi+1are adjacent

    {A, B, C, E} ?

    {E,C,B,A}?

    {A,B,C,B,E}?

    simple path: no repeated vertices, except possibly the first and the

    last

    {A, B, C, E} ? {E,C,B,A}?{A,B,C,B,E}?

    Cycle path: simple path, except that the last vertex is the same as

    the first vertex

    {B,C,D,E,B}?

  • 8/12/2019 DataStructure - Graphs

    12/24

    Definition and Terminology

    Weighted graph:When a no. or weight is associate with every

    edge, is called weighted graph. Weight are

    some times called cost. Source nodes:

    The node that have a positive out degree but

    zero in degree is called source node.

    Sink nodes:

    The node that has zero out degree but a positive

    in degree.12

  • 8/12/2019 DataStructure - Graphs

    13/24

    13/24

    Each edge ehas a weight, wt(e)

    graph may be undirected or directedweight may represent length, cost, capacity, etc

    adjacency matrix becomes weight matrix

    adjacency lists include weight in node

    4

    6

    6 7

    4

    5

    75

    5

    8

    v w

    yx

    z

    a weighted graphG

    Weighted Graphs

  • 8/12/2019 DataStructure - Graphs

    14/24

    14

    Definition and Terminology

    Connectivity

    Connected undirected graph: any two vertices are

    connected by some path in undirected graph

    Directed graph

    Strongly connected: A directed graph is strongly connected if

    there is a pathfrom each vertex to every other vertexin the

    digraph.

    Weakly connected: A directed graph is weakly connected if at

    most two vertices are not connected.

    Disjoint: A graph is disjoint if it is not connected.

    connected not connected

  • 8/12/2019 DataStructure - Graphs

    15/24

    15

    Definition and Terminology

    Degree in undirected graphThe degreeof a vertex is the number of edges

    incident to that vertex

    Example:

    The degrees of the nodes A, C, D, F = 1

    The degrees of the nodes B, E = 3

  • 8/12/2019 DataStructure - Graphs

    16/24

    16

    Definition and Terminology

    Degree in directed graph

    The degreeof a vertex is thesum of the

    indegree and outdegreeof lines incident to it.

    The outdegreeof a vertex in a digraph is thenumber of arcs leavingthe vertex.

    Theindegreeis the number of arcs enteringthe

    vertex.

    Example: B&E

  • 8/12/2019 DataStructure - Graphs

    17/24

    17

    Max and Min Degree of the

    Graph

    Max Degree:The highest degree of the node in the graph is called the

    Max Degree on the Graph

    Min Degree:

    The minimum degree of the node in the graph is called the

    Min Degree on the Graph

  • 8/12/2019 DataStructure - Graphs

    18/24

    18

    Graph Traversal Techniques

    There are two standard graph traversal

    techniques:

    Depth-First Search(DFS)

    Breadth-First Search(BFS)

  • 8/12/2019 DataStructure - Graphs

    19/24

    19

    Graph Traversal (Contd.)

    In both DFS and BFS, the nodes of theundirected graph are visited in a systematic

    manner so that every node is visited exactlyone.

    Both BFS and DFS give rise to a tree:

    When a node x is visited, it is labeled as visited,and it is added to the tree

    If the traversal got to node x from node y, y isviewed as the parent of x, and x a child of y

  • 8/12/2019 DataStructure - Graphs

    20/24

    Graph searches A tree search starts at the root

    and explores nodes from there,looking for a goal node (anode that satisfies certainconditions, depending on the

    problem)

    For some problems, any goalnode is acceptable (Nor J); forother problems, you want a

    minimum-depth goal node,that is, a goal node nearest theroot (only J)L M N O P

    G

    Q

    H JI K

    FED

    B C

    A

    Goal nodes

  • 8/12/2019 DataStructure - Graphs

    21/24

    Depth-first searching A depth-first search (DFS)

    explores a path all the way to a

    leaf before backtracking and

    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 orderA B D E H L M N I O P C F G J

    K Q

    Nwill be found before JL M N O P

    G

    Q

    H JI K

    FED

    B C

    A

  • 8/12/2019 DataStructure - Graphs

    22/24

    22

    Depth-First Search

    DFS follows the following rules:1. Select an unvisited node x, visit it, and treat as the

    current node

    2. Find an unvisited neighbor of the current node, visit it,

    and make it the new current node;

    3. If the current node has no unvisited neighbors,

    backtrackto the its parent, and make that parent the

    new current node;

    4. Repeat steps 3 and 4 until no more nodes can be

    visited.

    5. If there are still unvisited nodes, repeat from step 1.

  • 8/12/2019 DataStructure - Graphs

    23/24

    Breadth-first searching 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 OP Q

    Jwill be found before NL M N O P

    G

    Q

    H JI K

    FED

    B C

    A

  • 8/12/2019 DataStructure - Graphs

    24/24

    24

    Breadth-First Search

    BFS follows the following rules:1. Select an unvisited node x, visit it, make it the root in a

    BFS tree being formed. Its level is called the current

    level.

    2. From each node z in the current level, in the order inwhich the level nodes were visited, visit all the

    unvisited neighbors of z. The newly visited nodes from

    this level form a new level that becomes the next

    current level.

    3. Repeat step 2 until no more nodes can be visited.

    4. If there are still unvisited nodes, repeat from Step 1.