You are on page 1of 16

DATA STRUCTURES & ALGORITHMS

SUMMATIVE

3
[M7_M8_M9-SUMMATIVE3] - ANSWER SHEET
TREE & BINARY TREE / GRAPH / SORTING ALGORITHMS

Group Name: Rubick Mid

Name Role
Jose Mari Hernaez Lead Programmer
Members: Mervin Kim Sumagui Programmer 2
Moses Angelo
Programmer 3
Samson
Section: 4-B
• PROGRAM SOLUTION (60 POINTS)

• REQUIREMENTS:

• You are required to use Codio. Teacher will check actual C++ project in Codio. Each member
must ensure that his Codio Project is updated and the same as what the group submits so that the
teacher may check any of the member’s work.

• In snipping images of your answers, make sure that the names of the files are visible. Properly
label the images as follows and should be pasted in the order stated below:

• Header File (.h file)

• Implementation File (.cpp)

• Main Program (.cpp)

• Show sample output for each operation. Show adequate outputs to demonstrate how your
program works.

Snip and paste your source codes and outputs below. Snip it directly from Codio IDE so that colors
of the codes are preserved for readability. Include additional pages if necessary.
Main.cpp

#include <iostream>
#include <bits/stdc++.h>
#include "Data.h"

using namespace std;

int main(){
int ch;
char ch2;
Graph g(7);

system("clear");
cout << "Graph Operations" << endl;
cout << "[1] Create Graph" << endl;
cout << "[2] Traversal" << endl;
cout << "[3] Find Path" << endl;
cout << "[4] Path Cost" << endl;
cout << "[5] Exit Program" << endl;
cout << "Enter choice: ";

cin >> ch;


if (ch == 1){
system("clear");
cout << "CREATE GRAPH" << endl;
cout << "[a] Adjacency List" << endl;
cout << "[b] Adjacency Matrix" << endl;
cout << "Enter choice: ";
cin >> ch2;
if(ch2=='a'){
system("clear");
g.addEdge(0, 5);
g.addEdge(0, 6);
g.addEdge(1, 3);
g.addEdge(1, 5);
g.addEdge(2, 4);
g.addEdge(3, 1);
g.addEdge(3, 2);
g.addEdge(3, 4);
g.addEdge(4, 0);
g.addEdge(4, 2);
g.addEdge(5, 0);
g.addEdge(6, 0);
g.addEdge(6, 3);
g.printGraph();
cout << endl << endl;
} else if(ch2=='b'){
system("clear");
g.addEdge2(0, 5);
g.addEdge2(0, 6);
g.addEdge2(1, 3);
g.addEdge2(1, 5);
g.addEdge2(2, 4);
g.addEdge2(3, 1);
g.addEdge2(3, 2);
g.addEdge2(3, 4);
g.addEdge2(4, 0);
g.addEdge2(4, 2);
g.addEdge2(5, 0);
g.addEdge2(6, 0);
g.addEdge2(6, 3);
g.printGraph2();
} else {
cout << "Invalid Input.";
exit(1);
}
} else if (ch == 2){
system("clear");
cout << "TRAVERSAL" << endl;
cout << "[a] BFS" << endl;
cout << "[b] DFS" << endl;
cout << "Enter choice: ";
cin >> ch2;
if(ch2=='a'){
int j;
system("clear");
cout << "TRAVERSAL" << endl;
g.addEdge(0, 5);
g.addEdge(0, 6);
g.addEdge(1, 3);
g.addEdge(1, 5);
g.addEdge(2, 4);
g.addEdge(3, 1);
g.addEdge(3, 2);
g.addEdge(3, 4);
g.addEdge(4, 0);
g.addEdge(4, 2);
g.addEdge(5, 0);
g.addEdge(6, 0);
g.addEdge(6, 3);
g.printGraph();

cout << endl << "BFS Traversal" << endl;


cout << "Input your preferred start: ";
cin >> j;
cout << endl;
g.BFS(j);
cout << endl << endl;
} else if(ch2=='b'){
system("clear");
cout << "TRAVERSAL" << endl;
g.addEdge(0, 5);
g.addEdge(0, 6);
g.addEdge(1, 3);
g.addEdge(1, 5);
g.addEdge(2, 4);
g.addEdge(3, 1);
g.addEdge(3, 2);
g.addEdge(3, 4);
g.addEdge(4, 0);
g.addEdge(4, 2);
g.addEdge(5, 0);
g.addEdge(6, 0);
g.addEdge(6, 3);
g.printGraph();

int k;

cout << endl << "DFS Traversal" << endl;


cout << "Input your preferred source: ";
cin >> k;
cout << endl;

g.DFS(k);

cout << endl << endl;


} else {
cout << "Invalid Input.";
exit(1);
}
} else if (ch == 3){
system("clear");
cout << "FIND PATH" << endl;
Graph g(7);
g.addEdge(0, 5);
g.addEdge(0, 6);
g.addEdge(1, 3);
g.addEdge(1, 5);
g.addEdge(2, 4);
g.addEdge(3, 1);
g.addEdge(3, 2);
g.addEdge(3, 4);
g.addEdge(4, 0);
g.addEdge(4, 2);
g.addEdge(5, 0);
g.addEdge(6, 0);
g.addEdge(6, 3);
g.printGraph();
cout << endl;

cout << "[0] London" << endl;


cout << "[1] Dublin" << endl;
cout << "[2] Edinburg" << endl;
cout << "[3] Cardiff" << endl;
cout << "[4] Birmingham" << endl;
cout << "[5] Lancaster" << endl;
cout << "[6] Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" << endl;
int u,v;
cout << "Enter the start (0-6): ";
cin >> u;
cout << endl;
cout << "Enter the destination (0-6): ";
cin >> v;
if (g.FindPath(u, v))
cout << "\nThere is a path available going from " << u << " to " << v << endl;
else
cout << "\nThere is no path available going from " << u << " to " << v << endl;

int temp;
temp = u;
u = v;
v = temp;
if (g.FindPath(u, v))
cout << "There is a path available back from " << u << " to " << v << endl;
else
cout << "There is no path available back from " << u << " to " << v << endl;
return 0;

}else if (ch == 4){


system("clear");
int st, en;
cout << "PATH COST" << endl;

cout << "Start (1-6): ";


cin >> st;
cout << "End (1-6): ";
cin>> en;
int cost[3][3] = { { 1, 2, 3 },
{ 4, 8, 2 },
{ 1, 5, 3 } };

cout << g.minCost(cost, st, en) << endl;

return 0;

} else if (ch == 5){


cout << "Closing Program.." << endl;
exit(1);

} else {
cout << "Invalid Input.";
exit(1);
}
}
// Jose Mari Hernaez - Lead Programmer
// Mervin Kim Sumagui Programmer 2
// Moses Angelo Samson - Programmer 3
Data.h

#pragma once
#include <list>
#include <iostream>
using namespace std;

class Graph
{
private:
int V;
list <int> *adj;
int **adj2;
void DFSUtil(int v, bool visited[]);
void BFSUtil(int s, bool visited[]);

public:
Graph(int);
void addEdge(int u, int v);
void addEdge2(int u, int v);
bool FindPath(int u, int v);
void printGraph();
void printGraph2();
void DFS(int v);
void BFS(int s);
int minCost(int cost[3][3], int m, int n);
};

// Jose Mari Hernaez - Lead Programmer


// Mervin Kim Sumagui Programmer 2
// Moses Angelo Samson - Programmer 3

Implementation file

#include <iostream>
#include <list>
#include <limits>
#include "Data.h"
using namespace std;

Graph::Graph(int x)
{
V = x;
adj = new list <int> [V];
adj2 = new int* [V];
for (int i = 0; i < V; i++)
adj2[i] = new int[V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
adj2[i][j] = 0;

bool Graph::FindPath(int s, int d){


if (s == d)
return true;
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while (!queue.empty()) {
s = queue.front();
queue.pop_front();
for (i = adj[s].begin(); i != adj[s].end(); ++i) {
if (*i == d){
return true;
}
if (!visited[*i]) {
visited[*i] = true;
queue.push_back(*i);
}
}
}
return false;
}

void Graph::addEdge(int u, int v)


{
adj[u].push_back(v);
adj[v].push_back(u);
}

void Graph::addEdge2(int u, int v)


{
adj2[u][v] = 1;
}

// A utility function to print the adjacency list


// representation of graph
void Graph::printGraph()
{
cout << "Adjacency List..." << endl;
for (int v = 0; v < V; ++v)
{
cout << "V[" << v << "]";
for (auto x : adj[v])
cout << " -> " << x;
cout << endl;
}
}

void Graph::printGraph2()
{
cout << "Adjacency Matrix..." << endl << endl;
cout << "\t";
for (int i = 0; i < V; i++)
cout << "V[" << i << "]" << "\t";
cout << endl;
for (int i=0; i<V; i++)
{
cout << "V[" << i << "]" << "\t";
for (int j = 0; j < V; j++)
cout << adj2[i][j] << "\t";
cout << endl;
}
cout << endl;
}

int min(int x, int y, int z);

int Graph::minCost(int cost[3][3], int m, int n)


{
if (n < 0 || m < 0)
return INT8_MAX;
else if (m == 0 && n == 0)
return cost[m][n];
else
return cost[m][n] +
min(minCost(cost, m - 1, n - 1),
minCost(cost, m - 1, n),
minCost(cost, m, n - 1));
}

int min(int x, int y, int z)


{
if (x < y)
return (x < z) ? x : z;
else
return (y < z) ? y : z;
}

void Graph::DFSUtil(int v, bool visited[])


{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";

// Recur for all the vertices adjacent


// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}

// DFS traversal of the vertices reachable from v.


// It uses recursive DFSUtil()
void Graph::DFS(int v)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;

// Call the recursive helper function


// to print DFS traversal

DFSUtil(v, visited);
for(int i=0; i< V; i++)
if (!visited[i])
DFSUtil(i, visited);

for (int i = 0; i < V; i++)


if (!visited[i])
cout << i << " ";

list<int>::iterator i;

for(i = adj[v].begin(); i != adj[v].end(); ++i){


if(!visited[*i]){
DFS(*i);
}
}
}

void Graph::BFS(int s)
{

// Mark all the vertices as not visited


bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;

list<int> queue;

BFSUtil(s, visited);
for (int i = 0; i < V; i++)
if (!visited[i])
BFSUtil(i, visited);

for (int i = 0; i < V; i++)


if (!visited[i])
cout << i << " ";

list<int>::iterator i;
while(!queue.empty()){
s = queue.front();
cout << s << " ";
queue.pop_front();

for(i = adj[s].begin(); i != adj[s].end(); ++i){


if(!visited[*i]){
visited[*i] = true;
queue.push_back(*i);
}
}
}
}

void Graph::BFSUtil(int s, bool visited[])


{
// Create a queue for BFS
list<int> queue;

// Mark the current node as visited and enqueue it


visited[s] = true;
queue.push_back(s);

// 'i' will be used to get all adjacent


// vertices of a vertex
list<int>::iterator i;

while (!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();

// Get all adjacent vertices of the dequeued


// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
• QUESTION AND ANSWER (10 POINTS)

Briefly answer the questions below. Avoid erasures. Do not forget to include the sources for all NON-
ORIGINAL IDEAS. Indicate the name of the member that contributed the answer.

• Generate the Adjacency List of the Graph below:

• Which do you think is a more efficient way of representing a graph, using Adjacency Matrix or
Adjacency List? Support your choice.

• When traversing all outgoing edges in sparse graphs, adjacency lists are preferable since they can
do it in time (d: degree of the node). Matrices, on the other hand, offer superior cache performance
than adjacency lists due to sequential access, hence scanning matrices makes more sense for
fairly dense graphs.

• Give real-life examples on when BFS and DFS are used.

Djikstra's Algorithm is an example of BFS, as the algorithm finds the shortest path from root to
destination. This Algorithm is used in applications like GPS tracking.

An example of DFSs are Maze Solving Algorithm, as it begins at the root and explores
unexplored areas, and from that search determines the path.

You might also like