0% found this document useful (0 votes)
2 views5 pages

Gautamass 4

The document outlines a worksheet for a computer science student, detailing a problem where unique values from two linked lists need to be identified and squared. It includes a C++ code implementation for detecting cycles in a directed graph using depth-first search (DFS). Additionally, it lists learning outcomes related to graph representation, DFS implementation, cycle detection, and modular programming.

Uploaded by

shubham kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Gautamass 4

The document outlines a worksheet for a computer science student, detailing a problem where unique values from two linked lists need to be identified and squared. It includes a C++ code implementation for detecting cycles in a directed graph using depth-first search (DFS). Additionally, it lists learning outcomes related to graph representation, DFS implementation, cycle detection, and modular programming.

Uploaded by

shubham kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Worksheet 3

Student Name: Gautam Kumar UID: 23BCS13589


Branch: BE - CSE Section/Group: 809 - B
Semester: 3rd Date of Performance: 06-11-
24
Subject Name: ADSA
Subject Code: 23CSH - 204

1.Aim:
Problem Statement:
Elena is a software engineer tasked with processing two large datasets stored in
separate linked lists. Her objective is to identify the unique values across both lists,
compute their squares, and present the information in a concise and efficient
manner. To optimize the solution, Elena decides to use a hash set to quickly
identify unique values and avoid duplicates

Help Elena to complete the task

Input format

The first line contains an integer n. the number of elements in the first linked list
The second line contains n integers separated by spaces, representing the elements
of the first linked list.
The third line contains an integer m, the number of elements in the second linked
list
The fourth line contains m integers separated by spaces, representing the elements
of the second linked list.
Output format

The first line should print the unique elements from theninion of both linked listis,
sorted in ascending order, and separated by spaces The second line should print the
squares of these unique elements, sorted in ascending order, and separated by
spaces write same in right way .
3.Procedure:

#include <iostream>
#include <vector>
using namespace std;

// Structure to represent an edge in the graph


struct Edge {
int to;
Edge(int _to) : to(_to) {}
};

// Function to perform depth-first search (DFS) and check for cycles


bool isCyclicUtil(const vector<vector<Edge>>& graph, int v, vector<bool>&
visited, vector<bool>& recStack) {
if (!visited[v]) {
// Mark the current vertex as visited and add it to the recursion stack
visited[v] = true;
recStack[v] = true;

// Recur for all vertices adjacent to this vertex


for (const Edge& e : graph[v]) {
if (!visited[e.to] && isCyclicUtil(graph, e.to, visited, recStack)) {
return true;
} else if (recStack[e.to]) {
return true;
}
}
}

// Remove the vertex from the recursion stack


recStack[v] = false;
return false;
}

// Function to check whether the given multistage graph is acyclic


bool isAcyclic(const vector<vector<Edge>>& graph, int n) {
// Initialize arrays to track visited vertices and recursion stack
vector<bool> visited(n, false);
vector<bool> recStack(n, false);

// Perform DFS for each vertex


for (int i = 0; i < n; ++i) {
if (isCyclicUtil(graph, i, visited, recStack)) {
return false; // Cycle detected
}
}

return true; // No cycles found


}

int main() {
int numVertices, numEdges;
cin >> numVertices;
cin >> numEdges;

// Create an adjacency list representation of the graph


vector<vector<Edge>> graph(numVertices);

// Input edges
for (int i = 0; i < numEdges; ++i) {
int from, to;
cin >> from >> to;
graph[from].push_back(Edge(to));
}

// Check whether the given multistage graph is acyclic


if (isAcyclic(graph, numVertices)) {
cout << "The multistage graph is acyclic.";
} else {
cout << "The multistage graph contains cycles.";
}

return 0;
}

4.Output:
5.Learning Outcome:
·write learning outcomes of this code

Certainly! Here are the learning outcomes from the code provided:

1.

Understanding Graph Representation: Learn how to represent a graph


using adjacency lists, which is a common and efficient way to store and
manage graphs in memory.

2.
3.

Implementing Depth-First Search (DFS): Gain practical experience in


implementing the DFS algorithm to traverse a graph. This foundational
technique is critical for many graph-related problems.

4.
5.

Cycle Detection in Graphs: Learn how to detect cycles in a directed graph


using recursion and a stack to keep track of the recursion path.
Understanding cycle detection is essential for various applications,
including checking the acyclicity of workflows, dependencies, and more.

6.
7.

Managing Recursion and Recursion Stack: Enhance skills in managing


recursive functions and using additional data structures (like recStack) to
keep track of the function's state during recursion.
8.
9.

Handling Input and Output: Practice handling standard input and output
in C++, which is essential for competitive programming and real-world
applications.

10.
11.

Graph Theory Concepts: Solidify your understanding of key graph theory


concepts such as vertices, edges, cycles, and graph traversal techniques.

12.
13.

Modular Programming: Learn to break down a problem into modular


functions (isCyclicUtil and isAcyclic), which makes the code more readable,
maintainable, and reusable.

You might also like