Question

1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int Al1-(12, 8, 51, 0, 3, 7, 19, 6, 4, 21), please write down the divide and conquer procedure of quick sort (step by step), include all left and right positions adjustment, and overwriting. b. Using C/C++, define two integer arrays; each has 30 integers within [o, 99]. Use loop and rand0 to assign random integers to the arrays; then implement Selection and Insertion and Quick sort algorithms to sort the two arrays in ascending order respectively (i.e. using selection to sort the 1st, and quick to sort the 2nd). For each array, print all array elements after sorting to verifty the results c. based on 2.b and the sorted array, define a function of the Binary Search algorithm, then input a number in main) and call the binary search function to search a number in the sorted array and output the result (Note, search in one of the two sorted arrays will be enough) The following specifications will be expected for each programming proieçt in this lass
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.a

using namespace std; //namespace

#include<iostream> // header files

#include<conio.h>

#include<stdlib.h>

int cost[10][10],i,j,k,ver,stk[10],top,v,visit[10],visited[10];// required variables

int main(){ // main function

int ed;

cout <<"Enter no of vertices:"; // getting number of vertices

cin >> ver;

cout <<"Enter no of edges:"; // getting number of edges

cin >> ed;

cout <<"\nEDGES \n"; // asking the edges

for(k=0; k<ed; k++){

cin >>i>>j;

cost[i][j]=1; // making 1 in the adjacent matrix

cost[j][i]=1;

}

cout <<"Enter initial vertex to traverse from:"; // asking initial vertex

cin >>v;

cout <<"DFS ORDER OF VISITED VERTICES:"; // priting order

cout << v <<" "; // starting for initial vertex

visited[v]=1;

k=0;

while(k<ver) {// for every vertex

for(j=ver-1; j>=0; j--)// checking neighbour for every vertex

if(cost[ver][j]!=0 && visited[j]!=1 && visit[j]!=1) { //if neighbour

visit[j]=1; // make the neighbour as visited

stk[top++]=j; // push into stack

j=0; // break loop

}

ver=stk[--top]; // take vertex from top of stack

cout<<ver << " "; // print it

k++;

visit[ver]=0; // reapeat the same procedure

visited[ver]=1;

}

getch();

return 0;

}

Output;

Enter no of vertices:8 Enter no of edges : 11 EDGES 0 1 0 2 0 3 1 5 2 4 3 4 3 5 4 5 4 6 6 7 Enter initial vertex to traverse from: 2 DFS ORDER OF VISITED VERTICES:2 4 6 7 3 5 1 e

1.b

2.a

2.b

#include<stdio.h> // header files

#include<conio.h>

void sel(int *a){ // function for sorting (selection sort)

int i,j,loc,temp,min; // declaring required variables

for(i=0;i<30;i++){

min=a[i]; // taking first value of array in min

loc=i; // index number storing in loc

for(j=i+1;j<30;j++){ // from 2nd to last element

if(min>a[j]){ // checking the min of all the ellements comapring with min

min=a[j]; // getting the smallest of all

loc=j; // and index number

}

}

temp=a[i]; // swapping those 2 elements

a[i]=a[loc];

a[loc]=temp;

}

printf("\n\nSorted list after selection sort is as follows\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a[i]);

}

void qs(int a[],int low,int high){

int pivot;

if(low<high){ / Termination condition! /

pivot=partition(a,low,high); // Divide

qs(a,low,pivot-1);

qs(a,pivot+1,high); // conquer

}

}

int partition(int a[],int low,int high){

int pivot_item,left,right,temp;

pivot_item=a[low];

left=low;

right=high+1;

do {

do

left++; / Move left while item < pivot /

while(a[left]<pivot_item&&left<=high);

do

right--; / Move right while item > pivot /

while(pivot_item<a[right]);

if(left<right) {

temp=a[left];

a[left]=a[right];

a[right]=temp;

}

}while(left<right);

a[low]=a[right]; / right is final position for the pivot /

a[right]=pivot_item;

return(right);

}

main(){

int a1[30],i,a2[30]; // decalring 2 arrays

for (i=0;i<30;i++){ // for size of array

a1[i]=rand()%99; // fill the arrays with random elemenst in range 1 to 100

a2[i]=rand()%99;

}

printf("\nArray 1\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a1[i]);

printf("\nArray 2\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a2[i]);

sel(a1);

qs(a2, 0, 29);

printf("\n\nSorted list after quick sort is as follows\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a2[i]);

getch();

}

Output:

2.c

#include<stdio.h> // header files

#include<conio.h>

void binary(int a[],int s) { // function to search an element in an array

int l = 0, h = 30,mid; // initial variables

while (l <= h) { // termination condition

mid = (l+h)/2; // calculating middle value of the array

if (a[mid] == s) { // if the searched element is the middle value

printf("\nSearch Element : %d : Found at Position : %d.\n", s, mid+1); // print it

break;

}

else if (a[mid] < s) // elese in 1st half of the array

l = mid + 1;

else

h = mid - 1; // or 2nd part of the array

}

if (l > h)

printf("\nSearch Element : %d, Not Found \n", s);

}

void sel(int *a){ // function for sorting (selection sort)

int i,j,loc,temp,min; // declaring required variables

for(i=0;i<30;i++){

min=a[i]; // taking first value of array in min

loc=i; // index number storing in loc

for(j=i+1;j<30;j++){ // from 2nd to last element

if(min>a[j]){ // checking the min of all the ellements comapring with min

min=a[j]; // getting the smallest of all

loc=j; // and index number

}

}

temp=a[i]; // swapping those 2 elements

a[i]=a[loc];

a[loc]=temp;

}

printf("\n\nSorted list after selection sort is as follows\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a[i]);

}

void qs(int a[],int low,int high){

int pivot;

if(low<high){ / Termination condition! /

pivot=partition(a,low,high); // Divide

qs(a,low,pivot-1);

qs(a,pivot+1,high); // conquer

}

}

int partition(int a[],int low,int high){

int pivot_item,left,right,temp;

pivot_item=a[low];

left=low;

right=high+1;

do {

do

left++; / Move left while item < pivot /

while(a[left]<pivot_item&&left<=high);

do

right--; / Move right while item > pivot /

while(pivot_item<a[right]);

if(left<right) {

temp=a[left];

a[left]=a[right];

a[right]=temp;

}

}while(left<right);

a[low]=a[right]; / right is final position for the pivot /

a[right]=pivot_item;

return(right);

}

main(){

int a1[30],i,a2[30],s; // decalring 2 arrays

for (i=0;i<30;i++){ // for size of array

a1[i]=rand()%99; // fill the arrays with random elemenst in range 1 to 100

a2[i]=rand()%99;

}

printf("\nArray 1\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a1[i]);

printf("\nArray 2\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a2[i]);

sel(a1);

qs(a2, 0, 29);

printf("\n\nSorted list after quick sort is as follows\n");

for(i=0;i<30;i++) // printing the sorted list

printf("%d ",a2[i]);

printf("\nEnter the number to be search: ");

scanf("%d",&s);// getting number to search

binary(a2,s);

getch();

}

Output:

Add a comment
Know the answer?
Add Answer to:
1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using class) to traverse the graph. b. Similarly, implement BFS (define queue using class) to traverse the graph c.When node 6 is printed, what numbers are in the stack (for DFS) and queue (for BFS) respectively? Draw pictures to show them. 1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement...

    in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • 1. Solve synchronization problems inherent in inter-process communication and multi-threaded applications.

    1. Process control system calls: The demonstration of fork, execve and wait system calls along with zombie and orphan states.a. Implement the C program in which main program accepts the integers to besorted. Main program uses the fork system call to create a new process called a child process. Parent process sorts the integers using merge sort and waits for child process using wait system call to sort the integers using quick sort. Also demonstrate zombie and orphan states.b. Implement...

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • 1. What is the minimum number of locations a binary search algorithm will have to examine...

    1. What is the minimum number of locations a binary search algorithm will have to examine when looking for a particular value in a sorted array of 200 elements? (2 points) 1 6 7 8 200 2. In general, which of the following sorting algorithms is the MOST efficient? (2 points) Bubble sort Insertion sort Heap sort Merge sort Selection sort 3. Which of the following are quadratic-sorting algorithms? (2 points) I. insertion sort II. selection sort III. merge sort...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT