SlideShare a Scribd company logo
1 of 4
Write a c++ function to implement Dijkstra's algorithm on a graph. The function takes two
parameters: starting vertex and destination vertex. Print the shortest path and the shortest
distance from the starting vertex to the destination vertex.
void Graph::Dijkstra(string starting, string destination);
The vertex structure has been changed to include "visited", "distance" and "previous".
Solution
This was the best and optimized code for this questions and consider this one.
#include<iostream>
#include<stdio.h>
using namespace std;
#include<conio.h>
#define INFINITY 999
struct node
{
int cost;
int value;
int from;
}a[7];
void min_heapify(int *b, int i, int n)
{
int j, temp;
temp = b[i];
j = 2 * i;
while (j <= n)
{
if (j < n && b[j + 1] < b[j])
{
j = j + 1;
}
if (temp < b[j])
{
break;
}
else if (temp >= b[j])
{
b[j / 2] = b[j];
j = 2 * j;
}
}
b[j / 2] = temp;
return;
}
void build_minheap(int *b, int n)
{
int i;
for(i = n / 2; i >= 1; i--)
{
min_heapify(b, i, n);
}
}
void addEdge(int am[][7], int src, int dest, int cost)
{
am[src][dest] = cost;
return;
}
void bell(int am[][7])
{
int i, j, k, c = 0, temp;
a[0].cost = 0;
a[0].from = 0;
a[0].value = 0;
for (i = 1; i < 7; i++)
{
a[i].from = 0;
a[i].cost = INFINITY;
a[i].value = 0;
}
while (c < 7)
{
int min = 999;
for (i = 0; i < 7; i++)
{
if (min > a[i].cost && a[i].value == 0)
{
min = a[i].cost;
}
else
{
continue;
}
}
for (i = 0; i < 7; i++)
{
if (min == a[i].cost && a[i].value == 0)
{
break;
}
else
{
continue;
}
}
temp = i;
for (k = 0; k < 7; k++)
{
if (am[temp][k] + a[temp].cost < a[k].cost)
{
a[k].cost = am[temp][k] + a[temp].cost;
a[k].from = temp;
}
else
{
continue;
}
}
a[temp].value = 1;
c++;
}
cout<<"Cost"<<"t"<<"Source Node"<<endl;
for (j = 0; j < 7; j++)
{
cout<<a[j].cost<<"t"<<a[j].from<<endl;
}
}
int main()
{
int n, am[7][7], c = 0, i, j, cost;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
am[i][j] = INFINITY;
}
}
while (c < 12)
{
cout<<"Enter the source, destination and cost of edge ";
cin>>i>>j>>cost;
addEdge(am, i, j, cost);
c++;
}
bell(am);
getch();
}

More Related Content

Similar to Write a c++ function to implement Dijkstra-'s algorithm on a graph- Th.docx

OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++Allan Sun
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacionJeff Tu Pechito
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامAram Jamal
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh Singh
 
include.docx
include.docxinclude.docx
include.docxNhiPtaa
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFTimur Safin
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesSubhajit Sahu
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 

Similar to Write a c++ function to implement Dijkstra-'s algorithm on a graph- Th.docx (20)

C++ L07-Struct
C++ L07-StructC++ L07-Struct
C++ L07-Struct
 
Asssignment2
Asssignment2 Asssignment2
Asssignment2
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 
7720
77207720
7720
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارام
 
Cpp programs
Cpp programsCpp programs
Cpp programs
 
C++ programs
C++ programsC++ programs
C++ programs
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
Lab Question
Lab QuestionLab Question
Lab Question
 
include.docx
include.docxinclude.docx
include.docx
 
week-3x
week-3xweek-3x
week-3x
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
 
C programs
C programsC programs
C programs
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 

More from noreendchesterton753

Suppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docxSuppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docxnoreendchesterton753
 
Suppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docxSuppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docxnoreendchesterton753
 
Suppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docxSuppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docxnoreendchesterton753
 
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docxSuppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docxnoreendchesterton753
 
Suppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docxSuppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docxnoreendchesterton753
 
Suppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docxSuppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docxnoreendchesterton753
 
Suppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docxSuppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docxnoreendchesterton753
 
Supoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docxSupoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docxnoreendchesterton753
 
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docxSunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docxnoreendchesterton753
 
Sunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docxSunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docxnoreendchesterton753
 
Successful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docxSuccessful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docxnoreendchesterton753
 
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docxSubsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docxnoreendchesterton753
 
Substances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docxSubstances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docxnoreendchesterton753
 
subject FASHION The combination of which Roman terms most closely co.docx
subject FASHION   The combination of which Roman terms most closely co.docxsubject FASHION   The combination of which Roman terms most closely co.docx
subject FASHION The combination of which Roman terms most closely co.docxnoreendchesterton753
 
Styling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docxStyling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docxnoreendchesterton753
 
Students spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docxStudents spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docxnoreendchesterton753
 
Species interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docxSpecies interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docxnoreendchesterton753
 
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docxSpongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docxnoreendchesterton753
 
SPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docxSPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docxnoreendchesterton753
 
Starting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docxStarting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docxnoreendchesterton753
 

More from noreendchesterton753 (20)

Suppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docxSuppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docx
 
Suppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docxSuppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docx
 
Suppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docxSuppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docx
 
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docxSuppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docx
 
Suppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docxSuppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docx
 
Suppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docxSuppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docx
 
Suppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docxSuppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docx
 
Supoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docxSupoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docx
 
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docxSunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
 
Sunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docxSunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docx
 
Successful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docxSuccessful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docx
 
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docxSubsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docx
 
Substances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docxSubstances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docx
 
subject FASHION The combination of which Roman terms most closely co.docx
subject FASHION   The combination of which Roman terms most closely co.docxsubject FASHION   The combination of which Roman terms most closely co.docx
subject FASHION The combination of which Roman terms most closely co.docx
 
Styling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docxStyling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docx
 
Students spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docxStudents spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docx
 
Species interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docxSpecies interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docx
 
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docxSpongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
 
SPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docxSPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docx
 
Starting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docxStarting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docx
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Write a c++ function to implement Dijkstra-'s algorithm on a graph- Th.docx

  • 1. Write a c++ function to implement Dijkstra's algorithm on a graph. The function takes two parameters: starting vertex and destination vertex. Print the shortest path and the shortest distance from the starting vertex to the destination vertex. void Graph::Dijkstra(string starting, string destination); The vertex structure has been changed to include "visited", "distance" and "previous". Solution This was the best and optimized code for this questions and consider this one. #include<iostream> #include<stdio.h> using namespace std; #include<conio.h> #define INFINITY 999 struct node { int cost; int value; int from; }a[7]; void min_heapify(int *b, int i, int n) { int j, temp; temp = b[i]; j = 2 * i; while (j <= n) { if (j < n && b[j + 1] < b[j]) { j = j + 1; } if (temp < b[j]) {
  • 2. break; } else if (temp >= b[j]) { b[j / 2] = b[j]; j = 2 * j; } } b[j / 2] = temp; return; } void build_minheap(int *b, int n) { int i; for(i = n / 2; i >= 1; i--) { min_heapify(b, i, n); } } void addEdge(int am[][7], int src, int dest, int cost) { am[src][dest] = cost; return; } void bell(int am[][7]) { int i, j, k, c = 0, temp; a[0].cost = 0; a[0].from = 0; a[0].value = 0; for (i = 1; i < 7; i++) { a[i].from = 0; a[i].cost = INFINITY; a[i].value = 0; } while (c < 7) { int min = 999; for (i = 0; i < 7; i++) { if (min > a[i].cost && a[i].value == 0) { min = a[i].cost; } else
  • 3. { continue; } } for (i = 0; i < 7; i++) { if (min == a[i].cost && a[i].value == 0) { break; } else { continue; } } temp = i; for (k = 0; k < 7; k++) { if (am[temp][k] + a[temp].cost < a[k].cost) { a[k].cost = am[temp][k] + a[temp].cost; a[k].from = temp; } else { continue; } } a[temp].value = 1; c++; } cout<<"Cost"<<"t"<<"Source Node"<<endl; for (j = 0; j < 7; j++) { cout<<a[j].cost<<"t"<<a[j].from<<endl; } } int main() { int n, am[7][7], c = 0, i, j, cost; for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { am[i][j] = INFINITY; }
  • 4. } while (c < 12) { cout<<"Enter the source, destination and cost of edge "; cin>>i>>j>>cost; addEdge(am, i, j, cost); c++; } bell(am); getch(); }