File - MCE-CSE

advertisement
UNIT V
GRAPHS
Representation of Graphs – Breadth-first search – Depth-first search – Topological sort –
Minimum Spanning Trees – Kruskal and Prim algorithm – Shortest path algorithm –
Dijkstra’s algorithm – Bellman-Ford algorithm – Floyd - Warshall algorithm.
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 an 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.
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?
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 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.
13. 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.
14. Name the different ways of representing a graph?
a. Adjacency matrix
b. Adjacency list
15. What is an undirected acyclic graph?
When every edge in an acyclic graph is undirected, it is called an undirected acyclic
graph. It is also called as undirected forest.
16. What are the two traversal strategies used in traversing a graph?
a. Breadth first search
b. Depth first search
17. What is meant by strongly and weekly 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.
 When a directed graph is not strongly connected but the underlying graph is
connected, then the graph is said to be weakly connected.
18. 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.
19. What do you mean by breadth first search (BFS)?
BFS performs simultaneous explorations starting from a common point and
spreading out independently.
20. Differentiate BFS and DFS.
No. DFS
BFS
1
Backtracking is possible from Backtracking is not possible
a dead end
2
Vertices
from
which The vertices to be explored are
exploration is incomplete are organized as a FIFO queue
processed in a LIFO order
3
Search is done
particular direction
in
one The vertices in the same level are
maintained parallely
21. 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.
22. What is Topological Sort?
Topological sort is a process of assigning a linear ordering to the vertices of a dag so that if
there is an arc from vertex i to j, then i appears before j in the linear ordering.
23. Define minimum Spanning tree
Suppose G=(V, E) is aconnected graph in which each edge (u, v) in E has a cost c(u,v)
attached to it, A Spanning tree for G is afree tree that connects all the vertices in V. the cost
of spanning tree is the sum of the cost of edges in the tree.
A typical application for minimum-cost spanning trees occurs in the design of
communication networks. A minimum-cost spanning tree represents communication network
that connects all the cities at a minimal cost.
24. Write short notes on PRIM’S algorithm
Start with 1-vertex tree and grow it into an n-vertex tree by repeatedly adding a vertex
and an edge. When there is a choice, add a least cost edge.
Suppose V={1,2,…,n}, Prim’s algorithm begins with a set U initialized to {1}. It then
“grows” a spanning tree, one edge at a time. At each step, it finds a shortest edge (u, v) that
connects U and V-U and then adds v, the vertex in V-U, to U. It repeats this step until U=V.
25. Write short notes on KRUSKAL algorithm
Given a connected graph G-(V, E), with V={1, 2, …, n} and a cost function c defined
on edges of E. Another way to construct a minimum-cost spanning tree for G is to start with
graph T= (V, Ø) consisting only of the n vertices of G and having no edges. Each vertex is
therefore in connected component by itself.
To build larger components, we examine edges from E, in order of increasing cost.

If the edge connects two vertices in two different connected components, then we add
the edge to T.

If the edge connects two vertices in the same component, then we discard the edge,
since it would cause a cycle. When all vertices are in one component, T is a
minimum-cost spanning tree for G.
26. Define Warshall algorithm.
Warshall's algorithm calculates the transitive closure by generating a sequence of n
matrices, where n is the number of vertices. Recall that a path in a simple graph can be
defined by a sequence of vertices.
27. Define Floyd’s algorithm
Floyd's Algorithm for the all pairs shortest path problem is simple and elegant., this is
called the Floyd-Warshall algorithm.
Recall that the all pairs shortest path problem is: given a graph, find the shortest path
between every pair of vertices in that graph.
28. Short notes in Dijkstra’s Shortest Path

To find the shortest path between points, the weight or length of a path is calculated
as the sum of the weights of the edges in the path.

A path is a shortest path is there is no path from x to y with lower weight.

Dijkstra's algorithm finds the shortest path from x to y in order of increasing distance
from x. That is, it chooses the first minimum edge, stores this value and adds the next
minimum value from the next edge it selects.

It starts out at one vertex and branches out by selecting certain edges that lead to new
vertices.

It is similar to the minimum spanning tree algorithm, in that it is "greedy", always
choosing the closest edge in hopes of an optimal solution.
29. Characteristics of a Shortest Path Algorithm:
Shortest Path Algorithm selects an arbitrary starting vertex and then branches out from the
tree constructed so far. Each of the nodes it visits could be in the tree, in the fringes or
unseen. The designators:
TREE - nodes in the tree constructed so far
FRINGE - not in the tree, but adjacent to some vertex in the tree
UNSEEN - all others
30. Difference between kruskal and prim algorithm
• Prim’s algorithm initializes with a node, whereas Kruskal’s algorithm initiates with an
edge.
• Prim’s algorithms span from one node to another while Kruskal’s algorithm select the
edges in a way that the position of the edge is not based on the last step.
• In prim’s algorithm, graph must be a connected graph while the Kruskal’s can function
on disconnected graphs too.
• Prim’s algorithm has a time complexity of O(V2), and Kruskal’s time complexity is
O(logV).
31. Define Bellman–Ford algorithm
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single
source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's
algorithm for the same problem, but more versatile, as it is capable of handling graphs in
which some of the edge weights are negative numbers.
32. 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.
Download