DGraph Class Implementation Guide
DGraph Class Implementation Guide
Status Finished
Started Monday, 25 November 2024, 12:38 PM
Completed Saturday, 7 December 2024, 10:43 PM
Duration 12 days 10 hours
Grade 10.00 out of 10.00 (100%)
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 1/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 1
Correct
Template class DGraph representing a directed graph with type T with the initialized frame. It has attribute VertexNode*
nodeList, which is the head of a singly linked list, representing a list of vertex of this graph.
This class inlcudes 2 classes: VertexNode and Edge.
- Class VertexNode representing a vertex in graph. It has some attributes:
+ T vertex: the vertex's value.
+ Edge* adList: a singly linked list representing the adjacent edges that have this vertex as their starting vertex (from).
- Class Edge representing an edge in graph. It has some attributes:
+ VertexNode* fromNode - VertexNode* toNode: represents the starting vertex (from) and ending vertex (to) of this edge.
+ float weight: edge's weight.
Requirements: In class VertexNode, implement methods getEdge, connectTo, addAdjacentEdge, and removeTo.
Descriptions for each method are provided below. Ensure that all four methods are fully implemented before checking.
public:
class VertexNode {
private:
T vertex;
Edge* adList; //list of adjacent edge of this vertex
VertexNode* next;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 2/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
public:
class Edge {
private:
VertexNode* fromNode;
VertexNode* toNode;
float weight;
Edge* next;
};
};
For example:
Test Result
node0->connectTo(node1, 12.3);
node0->connectTo(node2, 13.3);
node0->connectTo(node3, 14);
node1->connectTo(node3, 176);
delete node0;
delete node1;
delete node2;
delete node3;
Reset answer
1 template<class T>
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 3/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
1 template<class T>
2 ▼ typename DGraph<T>::Edge* DGraph<T>::VertexNode::getEdge(VertexNode* toNode) {
3 //TODO: Iterate through the adjacency list of this vertex
4 // checking if there exists an edge with this vertex as the starting vertex
5 // and "toNode" as the ending vertex.
6 // If not return nullptr, else return that edge.
7 Edge* current = this->adList;
8▼ while(current != nullptr){
9▼ if(current->toNode->vertex == toNode->vertex){
10 return current;
11 }
12 current = current->next;
13 }
14 return nullptr;
15 }
16
17 template<class T>
18 ▼ void DGraph<T>::VertexNode::addAdjacentEdge(Edge* newEdge) {
19 //TODO: add newEdge to adjacency list of this vertex.
20 newEdge->next = this->adList;
21 this->adList = newEdge;
22
23 }
24
25 template<class T>
26 ▼ bool DGraph<T>::VertexNode::connectTo(VertexNode* toNode, float weight) {
27 //TODO: get edge from this node to "toNode".
28 Edge* temp = this->getEdge(toNode);
29 //TODO: If the edge is not existed, create a new Edge and add it to the adjacency list.
30 // If the edge is existed, update its weight.
31 ▼ if(temp == nullptr){
32 this->adList = new Edge(this, toNode, weight, adList);
33 return true;
34 }
35 temp->weight = weight;
36 return false;
37 //TODO: Return true if a new Edge is created; otherwise, return false.
38
39
40 }
41
42 template<class T>
43 ▼ bool DGraph<T>::VertexNode::removeTo(VertexNode *toNode) {
44 //TODO: remove the edge with "toNode" as the ending vertex from this node's adjacency list.
45 Edge* temp = this->getEdge(toNode);
46 if(temp == NULL)
47 return false;
48
49 ▼ if (temp == this->adList){
50 this->adList = temp->next;
51 delete temp;
52 return true;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 4/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
node0->connectTo(node1, 12.3);
node0->connectTo(node2, 13.3);
node0->connectTo(node3, 14);
node1->connectTo(node3, 176);
delete node0;
delete node1;
delete node2;
delete node3;
delete nodeA;
delete nodeB;
delete nodeC;
delete nodeD;
delete nodeE;
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 5/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 2
Correct
Template class DGraph representing a directed graph with type T with the initialized frame. It has attribute VertexNode*
nodeList, which is the head of a singly linked list, representing list of vertex of this graph.
This class inlcudes 2 classes: VertexNode and Edge.
- Class VertexNode representing a vertex in graph. It has some attributes:
+ T vertex: the vertex's value.
+ Edge* adList: a singly linked list representing the adjacent edges that have this vertex as their starting vertex (from).
- Class Edge representing an edge in graph. It has some attributes:
+ VertexNode* fromNode - VertexNode* toNode: represents the starting vertex (from) and ending vertex (to) of this edge.
+ float weight: edge's weight.
Requirements: In class DGraph, implement methods getVertexNode, add and connect. Descriptions for each method are
provided below. Ensure that all three methods are fully implemented before checking.
Notes: You can use the methods from the previous exercises without needing to implement them again.
public:
class VertexNode {
private:
T vertex;
Edge* adList; //list of adjacent edge of this vertex
VertexNode* next;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 6/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
friend class DGraph;
public:
class Edge {
private:
VertexNode* fromNode;
VertexNode* toNode;
float weight;
Edge* next;
};
};
For example:
Test Result
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 7/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Test Result
Reset answer
1 template<class T>
2 ▼ typename DGraph<T>::VertexNode* DGraph<T>::getVertexNode(T vertex) {
3 //TODO: Iterate through the Node list of the graph
4 // check if any vertexNode contains vertex.
5 // If such a vertexNode exists, return it; otherwise, return nullptr.
6 VertexNode* access = this->nodeList;
7 while(access != nullptr)
8 ▼ {
9 if(access->vertex == vertex)
10 ▼ {
11 return access;
12 }
13 access = access->next;
14 }
15 return nullptr;
16 }
17 template<class T>
18 ▼ void DGraph<T>::add(T vertex) {
19 //TODO: create a new vertexNode with vertex.
20
21 //TODO: add new vertexNode to the end of Node list of the graph.
22
23 //TODO: increase the countVertex.
24 if(this->countVertex == 0)
25 ▼ {
26 this->countVertex = 1;
27 this->nodeList = new VertexNode(vertex);
28 return;
29 }
30
31 VertexNode* access = this->nodeList;
32 while(access->next != nullptr)
33 access = access->next;
34
35 access->next = new VertexNode(vertex);
36 this->countVertex++;
37
38 }
39 template <class T>
40 ▼ void DGraph<T>::connect(T from, T to, float weight) {
41 //TODO: get vertexNode with "from" and vertexNode with "to".
42
43 //TODO: If either of the two vertexNode objects does not exist,
44 // throw an exception: VertexNotFoundException("Vertex doesn't exist!").
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 8/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
45
46 //TODO: connect "from" vertexNode to "to" vertexNode.
47 // If a new edge is created, increase the countEdge.
48 VertexNode* nod1 = this->getVertexNode(from);
49 VertexNode* nod2 = this->getVertexNode(to);
50
51 if(nod1 == nullptr || nod2 == nullptr)
52 throw VertexNotFoundException("Vertex doesn't exist!");
graph.printGraph();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 9/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 3
Correct
Template class DGraph representing a directed graph with type T with the initialized frame. It has attribute VertexNode*
nodeList, which is the head of a singly linked list, representing list of vertex of this graph.
This class inlcudes 2 classes: VertexNode and Edge.
- Class VertexNode representing a vertex in graph. It has some attributes:
+ T vertex: the vertex's value.
+ Edge* adList: a singly linked list representing the adjacent edges that have this vertex as their starting vertex (from).
- Class Edge representing an edge in graph. It has some attributes:
+ VertexNode* fromNode - VertexNode* toNode: represents the starting vertex (from) and ending vertex (to) of this edge.
+ float weight: edge's weight.
Requirements: Implement methods removeEdge and removeVertex. Descriptions for each method are provided below.
Notes:
- The removeTo method is used to delete an edge that ends at the vertex "toNode" from the adjacency list of the current
vertex. Students should use this method when implementing removeEdge and removeVertex.
- You can use the methods from the previous exercises without needing to implement them again.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 10/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
VertexNode* next;
class Edge {
private:
VertexNode* fromNode;
VertexNode* toNode;
float weight;
Edge* next;
};
};
For example:
Test Result
graph.printGraph();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 11/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Test Result
Reset answer
graph.removeEdge(1,
2);
graph.removeEdge(4,
5);
graph.removeEdge(1,
3);
graph.printGraph();
graph.removeVertex(2);
graph.printGraph();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 13/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 4
Correct
Template class DGraph representing a directed graph with type T with the initialized frame. It has attribute VertexNode*
nodeList, which is the head of a singly linked list, representing list of vertex of this graph.
This class inlcudes 2 classes: VertexNode and Edge.
- Class VertexNode representing a vertex in graph. It has some attributes:
+ T vertex: the vertex's value.
+ Edge* adList: a singly linked list representing the adjacent edges that have this vertex as their starting vertex (from).
- Class Edge representing an edge in graph. It has some attributes:
+ VertexNode* fromNode - VertexNode* toNode: represents the starting vertex (from) and ending vertex (to) of this edge.
+ float weight: edge's weight.
Requirements: Implement methods shape, empty and clear. Descriptions for each method are provided below.
Notes: You can use the methods from the previous exercises without needing to implement them again.
public:
class VertexNode {
private:
T vertex;
Edge* adList; //list of adjacent edge of this vertex
VertexNode* next;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 14/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
public:
class Edge {
private:
VertexNode* fromNode;
VertexNode* toNode;
float weight;
Edge* next;
};
};
For example:
Test Result
graph.connect(1, 2, 40);
graph.connect(1, 3, 6.9);
graph.connect(4, 5, 27);
graph.connect(3, 2, 2.1);
graph.connect(1, 2, 11.2);
graph.connect(1, 3, 67);
graph.removeEdge(1, 2);
graph.removeEdge(4, 5);
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 15/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Test Result
graph.connect(1, 2, 40);
graph.connect(1, 3, 6.9);
graph.connect(4, 5, 27);
graph.connect(3, 2, 2.1);
graph.connect(1, 2, 11.2);
graph.connect(1, 3, 67);
graph.clear();
cout << graph.shape() << endl;
cout << (graph.empty() ? "Graph is empty!" : "Graph is not empty!") << endl;
Reset answer
1 template<class T>
2 ▼ string DGraph<T>::shape() {
3 //TODO: return the string with format: [Vertices: <numOfVertex>, Edges: <numOfEdge>]
4 stringstream os;
5 os <<"[Vertices: " << this->countVertex <<", Edges: " << this->countEdge << "]";
6 return os.str();
7 }
8 template<class T>
9 ▼ bool DGraph<T>::empty() {
10 //TODO: return if graph is empty (it doesn't have any vertex and edge)
11 return this->countVertex == 0;
12 }
13 template<class T>
14 ▼ void DGraph<T>::clear() {
15 //TODO: remove all edges and vertices of graph.
16 while(this->nodeList != nullptr)
17 ▼ {
18 Edge* temp = this->nodeList->adList;
19 while(temp != nullptr)
20 ▼ {
21 Edge* next = temp->next;
22 delete temp;
23 temp = next;
24 }
25 VertexNode* toDel = this->nodeList;
26 this->nodeList = this->nodeList->next;
27 delete toDel;
28 }
29 this->countEdge = 0;
30 this->countVertex = 0;
31 }
32
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 16/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
graph.connect(1, 2, 40);
graph.connect(1, 3, 6.9);
graph.connect(4, 5, 27);
graph.connect(3, 2, 2.1);
graph.connect(1, 2, 11.2);
graph.connect(1, 3, 67);
graph.removeEdge(1, 2);
graph.removeEdge(4, 5);
graph.connect(1, 2, 40);
graph.connect(1, 3, 6.9);
graph.connect(4, 5, 27);
graph.connect(3, 2, 2.1);
graph.connect(1, 2, 11.2);
graph.connect(1, 3, 67);
graph.clear();
cout << graph.shape() << endl;
cout << (graph.empty() ? "Graph is empty!" : "Graph is not
empty!") << endl;
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 17/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 5
Correct
#include <iostream>
#include <list>
using namespace std;
class Adjacency
{
private:
list<int> adjList;
int size;
public:
Adjacency() {}
Adjacency(int V) {}
void push(int data)
{
adjList.push_back(data);
size++;
}
void print()
{
for (auto const &i : adjList)
cout << " -> " << i;
}
void printArray()
{
for (auto const &i : adjList)
cout << i << " ";
}
int getSize() { return adjList.size(); }
int getElement(int idx)
{
auto it = adjList.begin();
advance(it, idx);
return *it;
}
};
For example:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 18/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Test Result
int V = 6; 0 1 2 3 4 5
int visited = 0;
Graph g(V);
Adjacency* arr = new Adjacency(V);
int edge[][2] = {{0,1},{0,2},{1,3},{1,4},{2,4},{3,4},{3,5},{4,5}};
arr = g.BFS(visited);
arr->printArray();
delete arr;
int V = 6; 2 0 4 1 3 5
int visited = 2;
Graph g(V);
Adjacency* arr = new Adjacency(V);
int edge[][2] = {{0,1},{0,2},{1,3},{1,4},{2,4},{3,4},{3,5},{4,5}};
arr = g.BFS(visited);
arr->printArray();
delete arr;
Reset answer
1 class Graph
2 ▼ {
3 private:
4 int V;
5 Adjacency *adj;
6
7 public:
8 Graph(int V)
9 ▼ {
10 this->V = V;
11 adj = new Adjacency[V];
12 }
13
14 void addEdge(int v, int w)
15 ▼ {
16 adj[v].push(w);
17 adj[w].push(v);
18 }
19
20 void printGraph()
21 ▼ {
22 for (int v = 0; v < V; ++v)
23 ▼ {
24 cout << "\nAdjacency list of vertex " << v << "\nhead ";
25 adj[v].print();
26 }
27 }
28
29 Adjacency *BFS(int v)
30 ▼ {
// i
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 19/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
31 // v is a vertex we start BFS
32 Adjacency* result = new Adjacency();
33 bool visited[V] = {false};
34 list<int> q ;
35 q.push_back(v);
36 visited[v] = true;
37 ▼ while(!q.empty()){
38 int curr = q.front();
39 q.pop_front();
40 result->push(curr);
41
42 ▼ for(int i = 0; i < adj[curr].getSize(); i++){
43 ▼ if(!visited[adj[curr].getElement(i)]){
44 q.push_back(adj[curr].getElement(i));
45 visited[adj[curr].getElement(i)] = true;
46 }
47 }
48 }
49 return result;
50 }
51
52
int V = 6; 0 1 2 3 4 5 0 1 2 3 4 5
int visited = 0;
Graph g(V);
Adjacency* arr = new Adjacency(V);
int edge[][2] = {{0,1},{0,2},{1,3},{1,4},{2,4},{3,4},{3,5},{4,5}};
arr = g.BFS(visited);
arr->printArray();
delete arr;
int V = 6; 2 0 4 1 3 5 2 0 4 1 3 5
int visited = 2;
Graph g(V);
Adjacency* arr = new Adjacency(V);
int edge[][2] = {{0,1},{0,2},{1,3},{1,4},{2,4},{3,4},{3,5},{4,5}};
arr = g.BFS(visited);
arr->printArray();
delete arr;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 20/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
int V = 8, visited = 5; 5 2 0 1 6 3 4 5 2 0 1 6 3 4
7 7
Graph g(V);
Adjacency *arr;
int edge[][2] = {{0,1}, {0,2}, {0,3}, {0,4}, {1,2}, {2,5}, {2,6}, {4,6},
{6,7}};
for(int i = 0; i < 9; i++)
{
\tg.addEdge(edge[i][0], edge[i][1]);
}
// g.printGraph();
// cout << endl;
arr = g.BFS(visited);
arr->printArray();
delete arr;
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 21/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 6
Correct
#include <iostream>
#include <list>
using namespace std;
class Adjacency
{
private:
list<int> adjList;
int size;
public:
Adjacency() {}
Adjacency(int V) {}
void push(int data)
{
adjList.push_back(data);
size++;
}
void print()
{
for (auto const &i : adjList)
cout << " -> " << i;
}
void printArray()
{
for (auto const &i : adjList)
cout << i << " ";
}
int getSize() { return adjList.size(); }
int getElement(int idx)
{
auto it = adjList.begin();
advance(it, idx);
return *it;
}
};
For example:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 22/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Test Result
int V = 8, visited = 0; 0 1 2 5 6 4 7 3
Graph g(V);
Adjacency *arr;
int edge[][2] = {{0,1}, {0,2}, {0,3}, {0,4}, {1,2}, {2,5}, {2,6}, {4,6}, {6,7}};
for(int i = 0; i < 9; i++)
{
g.addEdge(edge[i][0], edge[i][1]);
}
// g.printGraph();
// cout << endl;
arr = g.DFS(visited);
arr->printArray();
delete arr;
Reset answer
1 class Graph
2▼ {
3 private:
4 int V;
5 Adjacency *adj;
6 public:
7 Graph(int V)
8▼ {
9 this->V = V;
10 adj = new Adjacency[V];
11 }
12
13 void addEdge(int v, int w)
14 ▼ {
15 adj[v].push(w);
16 adj[w].push(v);
17 }
18
19 void printGraph()
20 ▼ {
21 for (int v = 0; v < V; ++v)
22 ▼ {
23 cout << "\nAdjacency list of vertex " << v << "\nhead ";
24 adj[v].print();
25 }
26 }
27
28 Adjacency *DFS(int v)
29 ▼ {
30 // v is a vertex we start DFS
31 bool* visited = new bool[this->V];
32 for(int i = 0 ; i < this->V ; ++i)
33 visited[i] = false;
34
35
36 list<int> st;
37 Adjacency* result = new Adjacency();
38
39 st.push_back(v);
40
41
42 while(!st.empty())
43 ▼ {
44 int curr = st.back();
45 st.pop_back();
46
47 if(!visited[curr])
48 {
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 23/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
48 ▼ {
49 visited[curr] = true;
50 result->push(curr);
51 ▼ for (int i = adj[curr].getSize() - 1; i >= 0; --i) {
52 int neighbor = adj[curr].getElement(i);
53 ▼ if (!visited[neighbor]) {
54 st.push_back(neighbor);
55 }
56 }
57 }
58
59 }
60 delete[]visited;
61 return result;
62 }
int V = 8, visited = 0; 0 1 2 5 6 4 7 0 1 2 5 6 4 7
3 3
Graph g(V);
Adjacency *arr;
int edge[][2] = {{0,1}, {0,2}, {0,3}, {0,4}, {1,2}, {2,5}, {2,6}, {4,6},
{6,7}};
for(int i = 0; i < 9; i++)
{
\tg.addEdge(edge[i][0], edge[i][1]);
}
// g.printGraph();
// cout << endl;
arr = g.DFS(visited);
arr->printArray();
delete arr;
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 24/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 7
Correct
The relationship between a group of people is represented by an adjacency-list friends. If friends[u] contains v, u and v
are friends.Friendship is a two-way relationship. Two people are in a friend group as long as there is some path of mutual
friends connecting them.
Example:
Note:
In this exercise, the libraries iostream, string, cstring, climits, utility, vector, list, stack, queue, map, unordered_map,
set, unordered_set, functional, algorithm have been included and namespace std is used. You can write helper functions
and class. Importing other libraries is allowed, but not encouraged.
For example:
Test Result
vector<vector<int>> graph { 3
{1},
{0, 2},
{1},
{4},
{3},
{}
};
cout << numberOfFriendGroups(graph);
Reset answer
vector<vector<int>> graph { 3 3
\t{1},
\t{0, 2},
\t{1},
\t{4},
\t{3},
\t{}
};
cout << numberOfFriendGroups(graph);
vector<vector<int>> graph { 0 0
};
cout << numberOfFriendGroups(graph);
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 26/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 8
Correct
bool isCyclic();
For example:
Test Result
if(g.isCyclic())
cout << "Graph contains cycle";
else
cout << "Graph doesn't contain cycle";
Reset answer
1 #include <iostream>
2 #include <vector>
3 #include <list>
4 using namespace std;
5 class DirectedGraph
6▼ {
7 int V;
8 vector<list<int>> adj;
9 public:
10 DirectedGraph(int V)
11 ▼ {
12 this->V = V;
13 adj = vector<list<int>>(V, list<int>());
14 }
15 void addEdge(int v, int w)
16 ▼ {
17 adj[v].push_back(w);
18 }
19 bool isCyclicUtil(int u , bool* visited, bool*reStack)
20 ▼ {
21 if(!visited[u])
22 ▼ {
23 visited[u] = true;
24 reStack[u] = true;
25
26 for(auto x : adj[u])
27 ▼ {
28 if(!visited[x] && isCyclicUtil(x, visited, reStack))
29 return true;
30 else if(reStack[x])
31 return true;
32 }
33 }
34 reStack[u] = false;
35 return false;
36 }
37 bool isCyclic()
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 27/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
37 bool isCyclic()
38 ▼ {
39 // Student answer
40 bool* visited = new bool[this->V], *reStack = new bool[this->V];
41 for(int i = 0 ; i < this->V; ++i)
42 ▼ {
43 visited[i] = false;
44 reStack[i] = false;
45 }
46
47 for(int i = 0 ; i < this->V;++i)
48 ▼ {
49 if(!visited[i] && isCyclicUtil(i,visited, reStack))
50 return true;
51 }
52
53 return false;
54 }
55 };
56
if(g.isCyclic())
\tcout << "Graph contains cycle";
else
\tcout << "Graph doesn't contain cycle";
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 28/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 9
Correct
void topologicalSort();
where Adjacency is a structure to store list of number. Note that, the vertex index starts from 0. To match the given answer, please always
traverse from 0 when performing the sorting.
#include <iostream>
#include <list>
using namespace std;
class Adjacency
{
private:
list<int> adjList;
int size;
public:
Adjacency() {}
Adjacency(int V) {}
void push(int data)
{
adjList.push_back(data);
size++;
}
void print()
{
for (auto const &i : adjList)
cout << " -> " << i;
}
void printArray()
{
for (auto const &i : adjList)
cout << i << " ";
}
int getSize() { return adjList.size(); }
int getElement(int idx)
{
auto it = adjList.begin();
advance(it, idx);
return *it;
}
};
And Graph is a structure to store a graph (see in your answer box). You could write one or more helping functions.
For example:
Test Result
Graph g(6); 5 4 2 3 1 0
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.topologicalSort();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 29/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Answer: (penalty regime: 0 %)
Reset answer
1 ▼ class Graph {
2 int V;
3 Adjacency* adj;
4 public:
5 ▼ Graph(int V){
6 this->V = V;
7 adj = new Adjacency[V];
8 }
9 ▼ void addEdge(int v, int w){
10 adj[v].push(w);
11 }
12
13 //Heling functions
14 void topologicalSortUtil(int v, bool visited[],list<int>& st)
15 ▼ {
16 visited[v] = true;
17
18 for(int i = 0 ; i < this->adj[v].getSize(); ++i)
19 if(!visited[adj[v].getElement(i)])
20 topologicalSortUtil(adj[v].getElement(i), visited,st);
21
22 st.push_back(v);
23 }
24 ▼ void topologicalSort(){
25 //TODO
26 list<int> st;
27 bool* visited = new bool[this->V];
28
29 for(int i = 0 ; i < this-> V; ++i)
30 visited[i] = false;
31
32 for(int i = 0 ; i < this->V; ++i)
33 ▼ {
34 if(!visited[i])
35 topologicalSortUtil(i, visited, st);
36
37 }
38
39 while(!st.empty())
40 ▼ {
41 cout<<st.back()<<" ";
42 st.pop_back();
43 }
44 }
45 };
46
Graph g(6); 5 4 2 3 1 0 5 4 2 3 1 0
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.topologicalSort();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 30/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 31/33
12/9/24, 11:37 AM Graph: Attempt review | BK-LMS
Question 10
Correct
Given a graph and a source vertex in the graph, find shortest paths from source to destination vertice in the given graph using
Dijsktra's algorithm.
Following libraries are included: iostream, vector, algorithm, climits, queue
For example:
Test Result
int n = 6; 10
int init[6][6] = {
{0, 10, 20, 0, 0, 0},
{10, 0, 0, 50, 10, 0},
{20, 0, 0, 20, 33, 0},
{0, 50, 20, 0, 20, 2},
{0, 10, 33, 20, 0, 1},
{0, 0, 0, 2, 1, 0} };
Reset answer
int n = 6; 10 10
int init[6][6] = {
\t{0, 10, 20, 0, 0, 0},
\t{10, 0, 0, 50, 10, 0},
\t{20, 0, 0, 20, 33, 0},
\t{0, 50, 20, 0, 20, 2},
\t{0, 10, 33, 20, 0, 1},
\t{0, 0, 0, 2, 1, 0} };
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4833769&cmid=470645 33/33