Lab Exercise Brute Force 3.2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Answer: Lab Exercise Brute Force

1. Brute-Force Algorithm

Company XYZ is a big company which is selling toys for children. Ahmad, as a project
manager in the company, has a new idea to create a new toy. He is responsible for starting a
new project in producing the new toy. Figure 3 shows the tasks that need to be completed for
the new project appointed to Ahmad.

Idea

Find
Build Sponsors
prototype
First
Release
Sell
Marketing Product

Bugfixing Setup Legal Profit


Entity

Figure 1: Tasks need to be completed for the new project

a) Based on the Figure 3, traverse all the tasks in Figure 1 by illustrating step by step activity
using Breadth-First Search and Depth-First Search Algorithm. You also need to show the
changed elements in the queue and stack (based on the algorithm used) every time each node
is traversed.

1
Let
Idea = 1; 2 3
Build Prototype = 2;
Find Sponsor = 3; 4
First Release = 4; 8
Marketing = 5; 5
Sell Product = 6; 7

Bugfixing = 7;
6
Setup Legal Entity = 8;
Profit = 9

9
Breadth-First Search

Step 1 : Set 1 as root, and put 1 into the queue.

Queue : 1
1

BFS :

Step 2 : Next, remove 1 from queue, and put node 2 and 3 below 1 into queue.

Queue : 2, 3
2 3

BFS : 1

Step 3 : Then visit and remove 2 from the queue, and slot in 4 into the queue.

Queue : 3, 4

3 4
BFS : 1,2

Step 4 : Next, visit and remove 3 from the queue, and slot in 8 into the queue. ( 8 is the only
node left after 3 since 2 is visited )

Queue : 4, 8

4 8
BFS : 1, 2, 3

Step 5 : Visit and remove 4 from the queue, and slot in 7 into the queue.

Queue : 8, 7

8 7
BFS : 1, 2, 3, 4
Step 6 : After that, visit and remove 8 from the queue, and slot in 5 and 6 into the queue.

Queue : 7, 5, 6

7 5 6
BFS : 1, 2, 3, 4, 8

Step 7 : Visit and remove 7 from the queue, since no node left after 7 we will proceed to next.
( 5 and 6 is the only node after 7 and its listed in the queue )

Queue : 5, 6

5 6
BFS : 1, 2, 3, 4, 8, 7

Step 8 : Since no node left after 5, we will remove it from the queue. ( no node left after 5 as 6
is listed already )

Queue : 6

6
BFS : 1, 2, 3, 4, 8, 7, 5

Step 9 : Proceed to visit and remove 6 from the list. Then, slot in 9 the only node left after 6.

Queue : 9

9
BFS : 1, 2, 3, 4, 8, 7, 5, 6

Step 10 : Finally, visit 9 and remove it from the list. Nothing left after 9 so the BFS is
completed.

Queue :

BFS : 1, 2, 3, 4, 8, 7, 5, 6, 9
Depth-First Search

Step 1 : Set 1 as root then push 1 into the


stack and mark as visited.
Step 3 : The node after 2 is 4. Push it into
stack and visit it.

4
1
2
1
DFS : 1

DFS : 1, 2, 4

Step 2 : Next, node below 1 is 2 and 3. We


pick 2 and push it in to the stack, visit and Step 4 : Then push 7 that below 4 into
mark as visited. stack. Visit and marked it.

7
4
2 2
1 1

DFS : 1, 2 DFS : 1, 2, 4, 7
Step 5 : There is 5 and 6 after 7. We pick 5 visit it.
as next node then push it into stack and

Step 7 : Push 9 into the stack who is after


6. Visit and mark it as visited.

5
9
7
6
4
5
2
7
1
4
2
DFS : 1, 2, 4, 7, 5 1

DFS : 1, 2, 4, 7, 5, 6, 9
Step 6 : After that, the node after 5 is 6,
push 6 into the stack and mark and visit it.

Step 8 : No node after 9, so remove 9 from


the stack and backtrack the previous one.

6
5
7
6
4
5
2
7
1
4
2
DFS : 1, 2, 4, 7, 5, 6 1

DFS : 1, 2, 4, 7, 5, 6, 9
Step 9 : Since after 6 also no node left, Step 11 : node 5 and 6 after 7 are visited as
remove 6 from the stack and visit 5 well, remove 7 from stack and back to 4.

5
7
4 4
2 2
1 1

DFS : 1, 2, 4, 7, 5, 6, 9 DFS : 1, 2, 4, 7, 5, 6, 9

Step 10 : all node after 5 are cleared as Step 12 : Node after 4 is 7 are visited, so
well, so we will remove 5 from the stack remove 4 from stack and visit 2.
and back to 7.

7
4 2
2 1
1

DFS : 1, 2, 4, 7, 5, 6, 9
DFS : 1, 2, 4, 7, 5, 6, 9
Step 13 : Node after 2 is 4 are visited, so Step 14 : Next, after 3 is 8 the final node.
remove 4 from stack and visit 1. Push it into the stack and visit it.

8
3
1 1

DFS : 1, 2, 4, 7, 5, 6, 9 DFS : 1, 2, 4, 7, 5, 6, 9, 3, 8

Step 14 : Since 2 after 1 is visited, left 3 Step 15 : hence, all node are visited, we
we will push 3 into the stack and visit it. will backtrack until the stack is empty to
ensure all are visited. Node after 8 are 5
and 6, is both visited so remove 8 from the
stack.

3
1

DFS : 1, 2, 4, 7, 5, 6, 9, 3 3
1

DFS : 1, 2, 4, 7, 5, 6, 9, 3, 8
Step 16 : Next, node after 3 is 2, visited as Step 17 : Lastly, node after 1 is 2 and 3.
well, so remove 3 from the stack. Both visited as well, so remove 1 from the
stack and check whether all node are in the
visited lane. Then the DFS are completed.

DFS : 1, 2, 4, 7, 5, 6, 9, 3, 8

DFS : 1, 2, 4, 7, 5, 6, 9, 3, 8

EXTRA TASK:
a. Develop a BFS code, test with the data in question number 1. Make sure your output is
similar to your BFS output that you have manually trace. Provide your code in this answer,
and comment each line in the code to show that you are understand the BFS.

b. Develop a DFS code, test with the data in question number 1. Make sure your output is
similar to your BFS output that you have manually trace. Provide your code in this answer,
and comment each line in the code to show that you are understand the DFS.
BFS code

#include<stdio.h>
#include<conio.h>
#define max 20

void create_graph(int, int);


void bfs(int);
void display();

int adj[max][max];
int n=9,cntQ=0,queue[10], front=1;
int parent[9], vtx;

int main() {
int choice;
int node, origin, destin;
create_graph(1,2);
create_graph(1,3);
create_graph(3,2);
create_graph(2,4);
create_graph(3,8);
create_graph(4,7);
create_graph(5,6);
create_graph(6,9);
create_graph(7,5);
create_graph(7,6);
create_graph(8,5);
create_graph(8,6);
display();

bfs(1);

getch();
}

void create_graph(int origin, int destin) {


adj[origin][destin] = 1;
}

void display() {
int i, j;
printf(" 1 2 3 4 5 6 7 8 9\n");
for (i = 1; i <= n; i++) {
printf("%d",i);
for (j = 1; j <= n; j++)
printf("%4d", adj[i][j]);
printf("\n");
}
}

void enQ(int num)


{
cntQ++;
queue[cntQ] = num;
}

int deQ()
{
int val;

val = queue[front];
front++;

return val;
}

void bfs(int root)


{
int vtx;

char visited[10]={'F','F','F','F','F','F','F','F','F','F'};

enQ(root);
visited[root] = 'T';

for(int r=1;r<=n;r++)
{
vtx = deQ();
for(int c=1;c<=n;c++)
{
if (adj[vtx][c]==1)
{
if(visited[c]=='F')
{
visited[c]='T';
enQ(c);
}
}
}
printf("%d ", vtx);
}
}

OUTPUT
DFS code

#include<stdio.h>
#include<conio.h>
#define max 20

void create_graph(int, int);


void dfs(int);
void display();

int adj[max][max];
int n=9,cntS=0,stack[10];
int parent[9], vtx;

int main() {
int choice;
int node, origin, destin;
create_graph(1,2);
create_graph(1,3);
create_graph(3,2);
create_graph(2,4);
create_graph(3,8);
create_graph(4,7);
create_graph(5,6);
create_graph(6,9);
create_graph(7,5);
create_graph(7,6);
create_graph(8,5);
create_graph(8,6);
display();

dfs(1);

getch();
}

void create_graph(int origin, int destin) {


adj[origin][destin] = 1;
}

void display() {
int i, j;
printf(" 1 2 3 4 5 6 7 8 9\n");
for (i = 1; i <= n; i++) {
printf("%d",i);
for (j = 1; j <= n; j++)
printf("%4d", adj[i][j]);
printf("\n");
}
}

void push(int num)


{
cntS++;
stack[cntS] = num;
}

int pop()
{
int val;

val = stack[cntS];
stack[cntS]=' ';
cntS--;

return val;
}

void dfs(int root)


{
int vtx;

char visited[10]={'F','F','F','F','F','F','F','F','F','F'};

push(root);
visited[root] = 'T';

for(int r=1;r<=n;r++)
{
vtx = pop();
for(int c=n;c>=1;c--)
{
if (adj[vtx][c]==1)
{
if(visited[c]=='F')
{
visited[c]='T';
push(c);
}
}
}
printf("%d ", vtx);
}
}
OUTPUT
b) Produce the topological sort for all tasks in the Figure 1 by illustrating detail step by step
activity.

1 > 2, 3; 2 > 4; 3 > 2, 8; 4 > 7; 5 > 6, 8; 6 > 9; 7 > 5, 6; 8 > 6; 9 > x

Visited

1 2 3 4 5 6 7 8 9
FALS FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
E

Step 1 : Pick 1 as the root. Change value visited to true and level become 1.

Node 1 2 3 4 5 6 7 8 9
Parent Root
Visite FALSE FALS FALSE FALS FALSE FALS FALSE FALS FALSE
d E E E E
Level 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

Step 2 : Below 1 is node 2 and 3. So, the value of parent of 2 and 3 become 1. Then, the level
becomes 2.

Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1
Visite TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
d
Level 1 2 2 ∞ ∞ ∞ ∞ ∞ ∞

Step 3 : Choose 2 to begin. Node 2 connect to 4. So, the value visited change to true. The
value of parent of 4 become 2. The level becomes 3.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2
Visited TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Level 1 2 2 3 ∞ ∞ ∞ ∞ ∞
Step 4 : after node 3 is 2 and 8. 2 is used. So, the value visited change to true. The value of
parent of 8 become 3. The level becomes 4.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 3
Visite TRUE TRUE TRUE FALS FALS FALS FALS FALSE FALSE
d E E E E
Level 1 2 2 3 ∞ ∞ ∞ 4 ∞

Step 5 : Then the next node will be 7. The value visited of 4 changes to true. Then, the value
of parent of 7 become 4. The level becomes 5.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 4 3
Visited TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
Level 1 2 2 3 ∞ ∞ 5 4 ∞

Step 6 : next, 8 connect to 6. The value visited of 8 changes to true. Then, the value of parent
of 6 become 8. The level becomes 6.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 8 4 3
Visited TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE
Level 1 2 2 3 ∞ 6 5 4 ∞

Step 7 : Then, 7 connect to 5 and 6. Since, 6 is used. The value visited of 7 changes to true.
Then, the value of parent of 5 become 7. The level becomes 7.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 7 8 4 3
Visite TRUE TRUE TRUE TRUE FALS FALSE TRUE TRUE FALSE
d E
Level 1 2 2 3 7 6 5 4 ∞

Step 8 : Node after 6 are 9. The value visited of 6 changes to true. Then, the value of parent of
9 become 6. The level becomes 8.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 7 8 4 3 6
Visite TRU TRU TRU TRU FALS TRU TRU TRU FALS
d E E E E E E E E E
Level 1 2 2 3 7 6 5 4 8

Step 9 : Node 5 connect to 6 and 8. Since 6 and 8 are used. Only the value of visited of 5
changes to true.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 7 8 4 3 6
Visited TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
Level 1 2 2 3 7 6 5 4 8

Step 10 : After node 9 there is nothing else. The value of visited of 9 changes to true.
Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 7 8 4 3 6
Visite TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
d
Level 1 2 2 3 7 6 5 4 8

Step 11 : The topology sort are finished.


Node 1 2 3 4 5 6 7 8 9
Parent Root 1 1 2 7 8 4 3 6
Visite TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
d
Level 1 2 2 3 7 6 5 4 8

c) Time complexity: O(n) --- O(n^2)

#include<stdio.h>
#include<stdlib.h>

#define MAX 100 1


int n; /*Number of vertices in the graph*/ 1
int adj[MAX][MAX]; /*Adjacency Matrix*/ 1
void create_graph();

int queue[MAX], front = -1,rear = -1; 3


void insert_queue(int v);
int delete_queue(); 1
int isEmpty_queue(); 1

int indegree(int v); 1

int main()
{
int
i,v,count,topo_order[MAX],indeg[MAX]; 5

create_graph(); 1

/*Find the indegree of each vertex*/


for(i=0;i<n;i++) n
{
indeg[i] = indegree(i); 1*n
if( indeg[i] == 0 ) 1*n
insert_queue(i); 1*n
}

count = 0; 1

while( !isEmpty_queue( ) && count < n ) n


{
v = delete_queue(); 1*n
topo_order[++count] = v; /*Add vertex v to 1*n
topo_order array*/
/*Delete all edges going fron vertex v
*/
for(i=0; i<n; i++) n*n
{
if(adj[v][i] == 1) 1*n2
{
adj[v][i] = 0; 1*n2
indeg[i] = indeg[i]-1; 1*n2
if(indeg[i] == 0) 1*n2
insert_queue(i); 1*n2
}
}
}

1
if( count < n )
{
1
printf("\nNo topological ordering
possible, graph contains cycle\n");
1
exit(1);
}
1
printf("\nVertices in topological order
are :\n");
for(i=1; i<=count; i++) n
printf( "%d ",topo_order[i] ); 1*n
printf("\n");

return 0;
}/*End of main()*/

void insert_queue(int vertex)


{
if (rear == MAX-1) 1
printf("\nQueue Overflow\n"); 1
else
{
Time complexity = 41+20n+5n2

You might also like