0% found this document useful (0 votes)
6 views8 pages

DAA 6,7 Practical

Uploaded by

ishaginde2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

DAA 6,7 Practical

Uploaded by

ishaginde2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

6.

Write a program to sort the university roll numbers of your class using Quick
sort method and determine the time required to sort the elements.

Algorithm:
Step 1: QUICKSORT (array A, start, end)

Step 2: {

Step 3: if (start < end)

Step 4: {

Step 5: p = partition (A, start, end)

Step 6: QUICKSORT (A, start, p - 1)

Step 7: QUICKSORT (A, p + 1, end)

Step 8: }

Step 9: }

Partition algorithm:
Step 1: PARTITION (array A, start, end)

Step 2: {

Step 3: pivot = A[end]

Step 4: i = start- 1

Step 5: for j = start to end - 1 {

Step 6: do if (A[j] < pivot) {

Step 7: then i = i + 1

Step 8: swap A[i] with A[j]

Step 9: }

Step 10:}

Step 11: swap A[i+1] with A[end]

Step 12: return i+1

Step 13: }
Code:
#include<iostream>

using namespace std;

int PARTITION(int a[], int p,int r){

int x=a[r];

int i=p-1;

for(int j=p;j<=r-1;j++){

if(a[j]<=x){

i=i+1;

swap(a[i],a[j]);

swap(a[i+1],a[r]);

return i+1;

void QUICKSORT(int a[],int p, int r){

if(p<r){ int q=PARTITION(a,p,r);

QUICKSORT(a,p,q-1);

QUICKSORT(a,q+1,r);

int main(){

cout<<"ISHA 2203461\n";

int n;

cout<<"Enter the Total Roll No.'s: ";

cin>>n;

int a[n];

cout<<"Enter the Roll No.:";

for(int i=0;i<n;i++){
cin>>a[i];}

QUICKSORT(a,0,n-1);

cout<<"Sorted List of Roll No.'s: ";

for(int i=0;i<n;i++){

cout<<a[i]<<" ";

OUTPUT:
7. Write a program to find shortest path from your home to college using
Dijkstra‘s algorithm.
Algorithm:
Step 1: Initialize the distance to the starting node as 0 and to all other nodes as infinity.
Step 2: Set the starting node as the current node.
Step 3: Mark all nodes as unvisited and add them to a priority queue (min-heap).
Step 4: For the current node, examine its unvisited neighbors. For each neighbor:
a. Calculate the distance from the start node to this neighbor.
b. If this new distance is less than the current known distance to this neighbor, update it.
Step 5: Once done with the neighbors, mark the current node as visited.
Step 6: Select the unvisited node with the smallest known distance as the new "current node"
and repeat steps 4-6.
Step 7: Continue until all nodes are visited or the shortest path to the destination is found.

Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits> // Include this header for INT_MAX
using namespace std;
#define V 4 // No of vertices
// Function to select the minimum vertex that is not yet processed
int selectMinVertex(vector<int>& value, vector<bool>& processed) {
int minimum = INT_MAX;
int vertex = -1; // Initialize to -1 to avoid uninitialized behavior
for (int i = 0; i < V; ++i) {
if (!processed[i] && value[i] < minimum)
{
vertex = i;
minimum = value[i];
} }
return vertex; }
// Function to print the shortest path from the source to a given vertex
void printPath(vector<int>& parent, int vertex) {
if (vertex == -1) return;
printPath(parent, parent[vertex]);
cout << vertex << " ";
}
// Dijkstra's algorithm to find the shortest path from start to target
void dijkstra(int graph[V][V], int start, int target) {
vector<int> parent(V, -1); // Stores the shortest path structure
vector<int> value(V, INT_MAX); // Keeps shortest path values to each vertex from source
vector<bool> processed(V, false); // TRUE -> Vertex is processed
parent[start] = -1; // Start node has no parent
value[start] = 0; // Start node has value=0 to get picked 1st
// Table header
cout << "\Isha 2203461\n";
cout << "------------------------------------------------------------\n";
cout << "Step\tVertex\tCurrent Distance\tPath\t\tTotal Weight\n";
cout << "------------------------------------------------------------\n";
// Include (V-1) edges to cover all V vertices
for (int i = 0; i < V - 1; ++i) {
// Select the best vertex by applying the greedy method
int U = selectMinVertex(value, processed);
if (U == -1) break; // If no valid vertex is found, exit early
processed[U] = true; // Include new vertex in the shortest path graph
// Print the current step details
cout << i + 1 << "\t\t" << U << "\t\t\t\t" << value[U] << "\t\t\t";
printPath(parent, U);
// Providing space between the path and the total weight
cout << "\t\t\t" << value[U] << "\n"; // Print the current total weight
// Relax adjacent vertices (not yet included in the shortest path graph)
for (int j = 0; j < V; ++j) {
/* 3 conditions to relax:
1. There is an edge from U to j.
2. Vertex j is not included in the shortest path graph.
3. The edge weight is smaller than the current edge weight.
*/
if (graph[U][j] != 0 && !processed[j] && value[U] != INT_MAX
&& (value[U] + graph[U][j] < value[j])) {
value[j] = value[U] + graph[U][j];
parent[j] = U; }
} }
// Print the distance and the path from source to the target
cout << "------------------------------------------------------------\n";
cout << "Shortest Path from Vertex " << start << " to Vertex " << target << ":\n";
cout << "Total Weight: " << value[target] << "\n";
cout << "Path: ";
printPath(parent, target);
cout << "\n------------------------------------------------------------\n";
}
int main() {
// Graph representation using adjacency matrix
int graph[V][V] = {
{0, 5, 4, 0}, // Connections from vertex 0
{5, 0, 0, 7}, // Connections from vertex 1
{4, 0, 0, 3}, // Connections from vertex 2
{0, 7, 3, 0} // Connections from vertex 3
};
int start = 0; // Home at vertex 0
int target = 3; // College at vertex 4
// Run Dijkstra's algorithm to find the shortest path from start to target
dijkstra(graph, start, target);
return 0;
}

Output:

You might also like