Professional Documents
Culture Documents
SUMMATIVE
3
[M7_M8_M9-SUMMATIVE3] - ANSWER SHEET
TREE & BINARY TREE / GRAPH / SORTING ALGORITHMS
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:
• 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"
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: ";
int k;
g.DFS(k);
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;
return 0;
} 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);
};
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;
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;
}
DFSUtil(v, visited);
for(int i=0; i< V; i++)
if (!visited[i])
DFSUtil(i, visited);
list<int>::iterator i;
void Graph::BFS(int s)
{
list<int> queue;
BFSUtil(s, visited);
for (int i = 0; i < V; i++)
if (!visited[i])
BFSUtil(i, visited);
list<int>::iterator i;
while(!queue.empty()){
s = queue.front();
cout << s << " ";
queue.pop_front();
while (!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
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.
• 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.
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.