DAA Exp - 3.1
DAA Exp - 3.1
Experiment-3.1
2. Algorithms:
(i)
Step: 1. Initialize an empty stack to store the topological order.
Step: 2. Initialize a set or array to keep track of visited vertices to avoid revisiting them.
Step: 3. For each unvisited vertex 'v' in the DAG, perform a DFS:
- Call the DFS function with 'v' as the starting vertex.
Step: 4. In the DFS function, do the following:
- Mark the current vertex as visited.
- Recursively visit all unvisited neighbors of the current vertex.
- Push the current vertex onto the stack when all its neighbors have been visited.
Step: 5. After DFS traversal of the entire graph, the stack will contain the topological order in
reverse order.
Step: 6. Create a result list and pop elements from the stack to construct the topological order in
the correct order.
Step: 7. Return the topological order as the output.
End of Algorithm.
(ii)
Step 1: Create a stack data structure to maintain the nodes to be explored.
Step 2: Initialize a 2D boolean matrix called 'visited' to track visited cells. Initialize all cells
as unvisited.
Step 3: Push the starting node (startX, startY) onto the stack.
Step 4: While the stack is not empty:
a) Pop the top node from the stack and mark it as visited.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
b) If the popped node is the goal node (goalX, goalY), return True as a path has been
found.
c) For each unvisited neighboring cell (newX, newY) that is a valid move:
i) Push the neighboring cell onto the stack.
Step 5: If the stack becomes empty and the goal node has not been reached, return False as
there is no path from (startX, startY) to (goalX, goalY).
End of Algorithm.
3. Code:
(i)
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Graph {
int V; // Number of vertices
vector<vector<int>> adj; // Adjacency list
public:
Graph(int vertices) : V(vertices) {
adj.resize(V);
}
}
}
result.push(v);
}
vector<int> topologicalOrder;
while (!result.empty()) {
topologicalOrder.push_back(result.top());
result.pop();
}
return topologicalOrder;
}
};
int main() {
// Create a directed acyclic graph
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
}
cout << endl;
return 0;
}
(ii)
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int maze[ROWS][COLS] = {
{0, 1, 0, 0, 0},
{0, 1, 1, 0, 1},
{0, 0, 0, 0, 0},
{1, 1, 1, 1, 0},
{0, 0, 0, 1, 0}
};
struct Node {
int x, y;
};
while (!st.empty()) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int x = current.x;
int y = current.y;
visited[x][y] = true;
int main() {
int startX = 0;
int startY = 0;
int goalX = 4;
int goalY = 4;
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Output:
(i)
(ii)
4. Time complexity:
(i) The time complexity of the topological sort algorithm is O(V + E), where V is the number
of vertices and E is the number of edges in the graph.
(ii) The time complexity of the DFS algorithm is O(V + E), where V is the number of vertices
(grid cells) and E is the number of edges (possible moves).
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING