data structures and algorithms 2marks-unit 4

download data structures and algorithms 2marks-unit 4

of 17

Transcript of data structures and algorithms 2marks-unit 4

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    1/17

    UNIT IV

    GRAPHS

    2-MARK QUESTION AND ANSWERS:

    1. Define Graph.

    A graph G consist of a nonempty set V which is a set of nodes of the graph, a set E which

    is the set of edges of the graph, and a mapping from the set for edge E to a set of pairs of

    elements of V. It can also be represented as G=(V, E).

    2. Define adjacent nodes.

    Any two nodes which are connected by an edge in a graph are called adjacent nodes. For

    example, if an edge x E is associated with a pair of nodes (u,v) where u, v V, then we say that

    the edge x connects the nodes u and v.

    3. What is a directed graph?

    A graph in which every edge is directed is called a directed graph.

    4. What is a undirected graph?

    A graph in which every edge is undirected is called a directed graph.

    5. What is a loop?

    An edge of a graph which connects to itself is called a loop or sling.

    6. What is a simple graph?

    A simple graph is a graph, which has not more than one edge between a pair of

    nodes than such a graph is called a simple graph.

    7. What is a weighted graph?

    A graph in which weights are assigned to every edge is called a weighted graph.

    8. Define outdegree of a graph?

    In a directed graph, for any node v, the number of edges which have v as their

    initial node is called the out degree of the node v.

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    2/17

    9. Define indegree of a graph?

    In a directed graph, for any node v, the number of edges which have v as their terminal

    node is called the indegree of the node v.

    10. Define path in a graph?

    The path in a graph is the route taken to reach terminal node from a starting node.

    11. What is a simple path? IT 2201 DATA STRUCTURES AND ALGORITHMS II IT

    R.Barona, AP/IT

    20

    A path in a diagram in which the edges are distinct is called a simple path. It is

    also called as edge simple.

    12. What is a cycle or a circuit?

    A path which originates and ends in the same node is called a cycle or circuit.

    13. What is an acyclic graph?

    A simple diagram which does not have any cycles is called an acyclic graph.

    14. What is meant by strongly connected in a graph?

    An undirected graph is connected, if there is a path from every vertex to every

    other vertex. A directed graph with this property is called strongly connected.

    15. When is a graph said to be weakly connected?

    When a directed graph is not strongly connected but the underlying graph is

    connected, then the graph is said to be weakly connected.

    16. Name the different ways of representing a graph?

    a. Adjacency matrix

    b. Adjacency list

    17. What is an undirected acyclic graph?

    When every edge in an acyclic graph is undirected, it is called an undirected

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    3/17

    acyclic graph. It is also called as undirected forest.

    18. What are the two traversal strategies used in traversing a graph?

    a. Breadth first search

    b. Depth first search

    19. What is a minimum spanning tree?

    A minimum spanning tree of an undirected graph G is a tree formed from graph

    edges that connects all the vertices of G at the lowest total cost.

    20. Name two algorithms two find minimum spanning tree

    Kruskals algorithm

    Prims algorithm

    21. Define graph traversals?

    Traversing a graph is an efficient way to visit each vertex and edge exactly once.

    22. List the two important key points of depth first search.

    i) If path exists from one node to another node, walk across the edge exploring

    the edge.

    ii) If path does not exist from one specific node to any other node, return to the

    previous node where we have been before backtracking.

    23. What do you mean by breadth first search (BFS)?

    BFS performs simultaneous explorations starting from a common point and

    spreading out independently.

    24. Differentiate BFS and DFS.

    No. DFS BFS

    1. Backtracking is possible from a

    dead end

    Backtracking is not possible

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    4/17

    2. Vertices from which exploration is

    incomplete are processed in a

    The vertices to be explored are

    organized as a

    3. Search is done in one particular

    direction

    The vertices in the same level are

    maintained

    25. What do you mean by tree edge?

    If w is undiscovered at the time vw is explored, then vw is called a tree edge and

    v becomes the parent of w.

    26. What do you mean by back edge?

    If w is the ancestor of v, then vw is called a back edge.

    27. Define biconnectivity.

    A connected graph G is said to be biconnected, if it remains connected after removal of

    any one vertex and the edges that are incident upon that vertex. A connected graph is

    biconnected, if it has no articulation points.

    28. What do you mean by articulation point

    If a graph is not biconnected, the vertices whose removal would disconnect the

    graph are known as articulation points.

    29. What do you mean by shortest path?

    A path having minimum weight between two vertices is known as shortest path,

    in which weight is always a positive number.

    30. Define Activity node graph.

    Activity node graphs represent a set of activities and scheduling constraints. Each node

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    5/17

    represents an activity (task), and an edge represents the next activity.

    31. Define adjacency list.

    Adjacency list is an array indexed by vertex number containing linked lists. Each node Vi

    the ith array entry contains a list with information on all edges of G that leave Vi. It is used to

    represent the graph related problems

    DIAGRAMMATIC REPRESENTATIONS OF GRAPH:

    Vertex

    A vertex (also called a node) is a fundamental part of a graph. It can have a name, whichwe will call the key. A vertex may also have additional information. We will call thisadditional information the payload.

    Edge

    An edge (also called an arc) is another fundamental part of a graph. An edge connects twovertices to show that there is a relationship between them. Edges may be one-way or two-way. If the edges in a graph are all one-way, we say that the graph is a directed graph, ora digraph. The class prerequisites graph shown above is clearly a digraph since you musttake some classes before others.

    Weight

    Edges may be weighted to show that there is a cost to go from one vertex to another. Forexample in a graph of roads that connect one city to another, the weight on the edge mightrepresent the distance between the two cities.

    With those definitions in hand we can formally define a graph. A graph can be represented

    by G where G=(V,E). For the graph G, V is a set of vertices and E is a set of edges. Each edge isa tuple (v,w) where w,vV. We can add a third component to the edge tuple to represent a weight.

    A subgraph s is a set of edges e and vertices v such that eEand vV.

    shows another example of a simple weighted digraph. Formally we can represent this graph as the

    set of six vertices:

    V={V0,V1,V2,V3,V4,V5}

    and the set of nine edges:

    E={(v0,v1,5),(v1,v2,4),(v2,v3,9),(v3,v4,7),(v4,v0,1),(v0,v5,2),(v5,v4,8),(v3,v5,3),(v5,v2

    ,1)}

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    6/17

    Path

    A path in a graph is a sequence of vertices that are connected by edges. Formally we would

    define a path as w1,w2,...,wn such that (wi,wi+1)E for all 1in1. The unweighted

    path length is the number of edges in the path, specifically n1. The weighted path length is

    the sum of the weights of all the edges in the path. For example inFigure 2the path

    from V3 to V1 is the sequence of vertices (V3,V4,V0,V1). The edges

    are {(v3,v4,7),(v4,v0,1),(v0,v1,5)}.

    Cycle

    A cycle in a directed graph is a path that starts and ends at the same vertex. For example,

    inFigure 2the path (V5,V2,V3,V5) is a cycle. A graph with no cycles is called an acyclic

    graph. A directed graph with no cycles is called a directed acyclic graph or a DAG. We will

    see that we can solve several important problems if the problem can be represented as a

    DAG.

    An Adjacency Matrix

    One of the easiest ways to implement a graph is to use a two-dimensional matrix. In this matrix

    implementation, each of the rows and columns represent a vertex in the graph. The value that is

    http://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimple
  • 7/27/2019 data structures and algorithms 2marks-unit 4

    7/17

    stored in the cell at the intersection of row v and column w indicates if there is an edge from

    vertex v to vertex w. When two vertices are connected by an edge, we say that they are adjacent

    An Adjacency Matrix Representation for a Graph

    An Adjacency ListA more space-efficient way to implement a sparsely connected graph is to use an adjacency list. In

    an adjacency list implementation we keep a master list of all the vertices in the Graph object and

    then each vertex object in the graph maintains a list of the other vertices that it is connected to. In

    our implementation of the Vertex class we will use a dictionary rather than a list where the dictionary

    keys are the vertices, and the values are the weights.Figure 4illustrates the adjacency list

    representation for the graph inFigure 2.

    http://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-adjlisthttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-adjlisthttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-adjlisthttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-dgsimplehttp://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#fig-adjlist
  • 7/27/2019 data structures and algorithms 2marks-unit 4

    8/17

    An Adjacency List Representation of a Graph

    The advantage of the adjacency list implementation is that it allows us to compactly represent a

    sparse graph. The adjacency list also allows us to easily find all the links that are directly connected

    to a particular vertex.

    Topological Sorting

    A topological sort takes a directed acyclic graph and produces a linear ordering of all its vertices

    such that if the graph G contains an edge (v,w)then the vertex v comes before the vertex w in the

    ordering. Directed acyclic graphs are used in many applications to indicate the precedence of

    events. Making pancakes is just one example; other examples include software project schedules,

    precedence charts for optimizing database queries, and multiplying matrices.

    http://interactivepython.org/courselib/static/pythonds/Graphs/graphdfs.html#topological-sortinghttp://interactivepython.org/courselib/static/pythonds/Graphs/graphdfs.html#topological-sorting
  • 7/27/2019 data structures and algorithms 2marks-unit 4

    9/17

    Figure 33: A Graph G

    Figure 34: Its Transpose GT

    Dijkstras Algorithm

    The algorithm we are going to use to determine the shortest path is called Dijkstras algorithm.

    Dijkstras algorithm is an iterative algorithm that provides us with the shortest path from oneparticular starting node to all other nodes in the graph. Again this is similar to the results of a breadth

    first search.

    PROBLEM SOLVING:

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    10/17

    Figure 3: Tracing Dijkstras Algorithm

    Figure 4: Tracing Dijkstras Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    11/17

    Figure 5: Tracing Dijkstras Algorithm

    Figure 6: Tracing Dijkstras Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    12/17

    Figure 7: Tracing Dijkstras Algorithm

    Figure 8: Tracing Dijkstras Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    13/17

    Prims Spanning Tree Algorithm

    Figure 11: Tracing Prims Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    14/17

    Figure 12: Tracing Prims Algorithm

    Figure 13: Tracing Prims Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    15/17

    Figure 14: Tracing Prims Algorithm

    Figure 15: Tracing Prims Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    16/17

    Figure 16: Tracing Prims Algorithm

    Figure 17: Tracing Prims Algorithm

  • 7/27/2019 data structures and algorithms 2marks-unit 4

    17/17

    SummaryIn this chapter we have looked at the graph abstract data type, and some implementations of a

    graph. A graph enables us to solve many problems provided we can transform the original problem

    into something that can be represented by a graph. In particular, we have seen that graphs areuseful to solve problems in the following general areas.

    Breadth first search for finding the unweighted shortest path. Dijkstras algorithm for weighted shortest path. Depth first search for graph exploration. Strongly connected components for simplifying a graph. Topological sort Minimum weight spanning trees