All Exams  >   Software Development  >   DSA in C++  >   All Questions

All questions of Graphs for Software Development Exam

Which of the following algorithms is used to find the shortest path between two vertices in a weighted graph?
  • a)
    Breadth First Search (BFS)
  • b)
    Depth First Search (DFS)
  • c)
    Dijkstra's algorithm
  • d)
    Topological sort
Correct answer is option 'C'. Can you explain this answer?

Ishani Chopra answered
Brief Explanation:

Dijkstra's algorithm is used to find the shortest path between two vertices in a weighted graph. It is an algorithm that works on graphs with non-negative edge weights. The algorithm maintains a priority queue of vertices and repeatedly selects the vertex with the smallest distance from the source. It then relaxes the edges from that vertex, updating the distance to each neighboring vertex if a shorter path is found.

Explanation:

Dijkstra's algorithm is a popular algorithm used in graph theory to find the shortest path between two vertices in a weighted graph. It is applicable to graphs with non-negative edge weights. The algorithm maintains a priority queue of vertices and repeatedly selects the vertex with the smallest distance from the source.

Key Steps:
1. Initialize the distance of all vertices from the source to infinity except for the source vertex, which is initialized to 0.
2. Create a priority queue to store the vertices and their corresponding distances.
3. Insert the source vertex into the priority queue.
4. While the priority queue is not empty, do the following steps:
- Remove the vertex with the smallest distance from the priority queue.
- For each neighboring vertex of the current vertex, calculate the distance from the source through the current vertex. If this distance is smaller than the previously calculated distance, update the distance and insert the vertex into the priority queue.
5. Repeat step 4 until the priority queue is empty.
6. After the algorithm terminates, the distances of all vertices from the source will be calculated. The shortest path from the source to any other vertex can be obtained by following the path of minimum distances.

Advantages and Limitations:
- Dijkstra's algorithm guarantees finding the shortest path between two vertices in a weighted graph.
- It is efficient for sparse graphs with non-negative edge weights.
- However, it does not work correctly for graphs with negative edge weights and may not be the most efficient algorithm for dense graphs.

In conclusion, Dijkstra's algorithm is the correct option for finding the shortest path between two vertices in a weighted graph.

In a directed graph, an edge from vertex A to vertex B means:
  • a)
    There is a directed edge from A to B
  • b)
    There is an undirected edge between A and B
  • c)
    There is no edge between A and B
  • d)
    None of the above
Correct answer is option 'A'. Can you explain this answer?

Tanishq Roy answered


Explanation:

The correct answer is option 'A' because in a directed graph, an edge from vertex A to vertex B indicates a directed edge from A to B. This means that there is a one-way connection or relationship from vertex A to vertex B.

Here is a detailed explanation:

Directed Graph:
- In a directed graph, each edge has a direction associated with it. This means that the connection between vertices is one-way.
- An edge from vertex A to vertex B denotes that there is a directed edge from A to B, allowing traversal from A to B but not necessarily from B to A.
- It is represented by an arrow pointing from the starting vertex (A) to the ending vertex (B).

Undirected Edge:
- An undirected edge represents a two-way connection between vertices. This means that the relationship between the vertices is bidirectional.
- In an undirected edge, the connection between vertices A and B can be traversed in both directions.

Conclusion:
- Therefore, in a directed graph, when an edge is specified from vertex A to vertex B, it signifies a directed edge from A to B, indicating a one-way relationship between the two vertices.

Which traversal algorithm uses a stack data structure to explore vertices?
  • a)
    Depth First Search (DFS)
  • b)
    Breadth First Search (BFS)
  • c)
    Dijkstra's algorithm
  • d)
    Prim's algorithm
Correct answer is option 'A'. Can you explain this answer?

Depth First Search (DFS) is the traversal algorithm that uses a stack data structure to explore vertices in a graph. It is a recursive algorithm that starts at a given vertex and explores as far as possible along each branch before backtracking.

DFS Algorithm Steps:
1. Create a stack and push the starting vertex onto the stack.
2. Mark the starting vertex as visited.
3. While the stack is not empty, do the following steps:
- Pop a vertex from the stack.
- Visit the popped vertex.
- Push all the adjacent vertices of the popped vertex onto the stack if they are not visited and mark them as visited.
- Repeat until the stack is empty.

Explanation:

Depth First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. The main idea behind DFS is to explore as far as possible along each branch before backtracking. It uses a stack data structure to keep track of the vertices to be explored.

The DFS algorithm starts at a given vertex and explores its adjacent vertices by following a path until it reaches a dead end. It then backtracks to the previous vertex and continues exploring other adjacent vertices. This process continues until all vertices have been visited.

The stack data structure is used to keep track of the vertices that need to be explored. When a vertex is visited, it is marked as visited and pushed onto the stack. The algorithm then pops a vertex from the stack, visits it, and pushes its unvisited adjacent vertices onto the stack. This process is repeated until the stack is empty, indicating that all vertices have been visited.

DFS is often used to solve problems like finding connected components in a graph, checking if a graph is cyclic, or finding a path between two vertices.

In conclusion, the Depth First Search (DFS) algorithm uses a stack data structure to explore vertices in a graph. It starts at a given vertex, explores as far as possible along each branch, and backtracks when necessary.

Chapter doubts & questions for Graphs - DSA in C++ 2026 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2026 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Graphs - DSA in C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

DSA in C++

153 videos|115 docs|24 tests

Top Courses Software Development