Lecture-19-CS210-2012.pptx

Embed Size (px)

Citation preview

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    1/21

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 19

    Graphs Definitions, notations, and terminologies

    Data structures for graphs

    A few algorithmic problems in graphs

    1

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    2/21

    Why Graphs ??

    2

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    3/21

    Finding shortest route between cities

    3

    Given a network of roadsconnecting various cities, design an algorithm to

    compute the shortest route between any two cities.

    Just imagine how you would solve/approach this problem.

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    4/21

    Embedding an integrated circuit on

    mother board

    4

    How would you embed portsof various ICs on a plane and make connectionsamong

    them so that

    No two connections intersect each other

    The total length of all the connections is minimal

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    5/21

    A social network or world wide web (WWW)

    5

    Can we make some useful observations about the structure (diameter, degree

    distribution, etc.) of such networks ?

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    6/21

    The three applications can be modeled as Graph

    VerticesEdges

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    7/21

    Graph

    7

    Definitions, notations, and terminologies

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    8/21

    Graph

    A graph Gis defined by two sets

    V : set of vertices

    E : set of edges

    Note that E (VV)

    Notation: A graph consisting of vertices V and edges E is denoted by G= (V,E)

    8

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    9/21

    Types of graphs

    Undirected Graph Directed Graph

    9

    V= {1,2,3,4,5,6}

    E= {(1,2), (1,5),

    (2,5), (2,3),

    (3,4),

    (4,5), (4,6)}

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

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

    (2,0), (2,1),

    (3,2),

    (4,5),

    (5,4) }

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    10/21

    Notations

    Notations:

    = |V|

    m= |E|

    Note: For directed graphs, m ??.

    For undirected graphs, m

    ??

    10

    (-1)

    (-1)/2

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    11/21

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    12/21

    Terminologies

    is a walkfrom1 to4.

    is nota walk.

    is a walkfrom 1to 6. is a pathfrom 1to 6.

    is a cycle.

    12

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    13/21

    Terminologies

    Connected component:

    A maximalsubset of connected vertices

    You can not add any more vertex to the subsetand still keeping it connected.

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    14/21

    Data Structures for Graphs

    14

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    15/21

    Link based data structure for graph

    Undirected Graph Adjacency Lists

    15

    V= {1,2,3,4,5,6}

    E= {(1,2), (1,5),

    (2,5), (2,3),

    (3,4),

    (4,5), (4,6)}

    1 3 5

    2 5

    2 4

    4

    3 5 6

    1 2 4

    1

    2

    3

    4

    5

    6

    Size = O(n+m)

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    16/21

    Link based data structure for graph

    Advantage of Adjacency Lists :

    Space efficient

    All the neighbors of a vertex can be computed in optimal time.

    Disadvantage of Adjacency Lists :

    To determine if there is an edge fromxto y, it may require scanning the

    entire adjacency list of vertexx(Hence O(n) time in the worst case).

    16

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    17/21

    Array based data structure for graph

    Undirected Graph Adjacency Matrix

    17

    V= {1,2,3,4,5,6}

    E= {(1,2), (1,5),

    (2,5), (2,3),

    (3,4),

    (4,5), (4,6)}

    1

    2

    3

    4

    5

    6

    0 1 0 0 1 0

    1 2 3 4 5 6

    1 0 1 0 1 0

    0 1 0 1 0 0

    0 0 1 0 1 1

    1 1 0 1 0 0

    0 0 0 1 0 0

    Size = O(2)

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    18/21

    Array based data structure for graph

    Advantage of Adjacency Matrix :

    It takes O(1) time to determine whether there is an edge fromxto y for

    any two verticesxand y.

    Disadvantage of Adjacency Matrix :

    It takes O() time to compute/enumerate all neighbors of a given vertexx.

    It takes O() space. So we can not store Adjacency Matrix in these days

    RAM even for graphs having 1 million vertices.

    18

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    19/21

    Which data structure is commonly used for

    storing graphs ?

    Majority of applications of graphs store them in adjacency lists mainly due to

    the following compelling reasons.

    Graphs in real life are sparse ( ). So Adjacency lists representation

    offers a much more compact storage compared to Adjacency matrixrepresentation.

    Most of the algorithms on graphs require enumerating and processing all

    neighbors of each vertex. As a result adjacency matrix will enforce O()

    bound on the time complexity for the algorithm irrespective of the othercomputation.

    **There are some exceptions where the algorithm does not require enumeration of all

    neighbors of each vertex. One such exception is discussed at the end of this lecture.

    19

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    20/21

    A sample of Graph algorithmic Problems

    Almost every second algorithm is a graph algorithms due to wide applications

    of graphs for modeling various computational problems. The student is

    encouraged to ponder over the following problems. All of them and many

    more will be discussed during this course and the course CS345.

    Determine if there is a path between any two given vertices in a graph.

    Compute all connected components in a graph.

    Determine if there is a cycle in a graph.

    Compute the largest possible set of edges from the graph whose removal

    does not destroy the connectivity of the graph Compute a path of shortest length between two vertices.

    Determine if there is a cycle passing through all vertices.

    Compute the cycle of shortest length in a graph.

    20

  • 8/11/2019 Lecture-19-CS210-2012.pptx

    21/21

    An interesting problem(Finding a sink)

    A vertexxin a given directed graph is said to be a sinkif

    There is no edge emanating from (or leaving)x

    Every other vertex has an edge intox.

    Given a directed graph G=(V,E) given in an adjacency matrix representation,

    design an O(n) time algorithm to determine if there is any sinkin G.

    You are encouraged to solve the problem mentioned above. It does not require

    anything more than what has been discussed in this lecture. We shall discuss its

    solution in the next class.

    21