shortest path with bfs in c++
C++
// CPP code for printing shortest path between
// two vertices of unweighted graph
#include <bits/stdc++.h>
using namespace std;
// utility function to form edge between two vertices
// source and dest
void add_edge(vector<int> adj[], int src, int dest)
{
adj[src].push_back(dest);
adj[dest].push_back(src);
}
// a modified version of BFS that stores predecessor
// of each vertex in array p
// and its distance from source in array d
bool BFS(vector<int> adj[], int src, int dest, int v,
int pred[], int dist[])
{
// a queue to maintain queue of vertices whose
// adjacency list is to be scanned as per normal
// DFS algorithm
list<int> queue;
// boolean array visited[] which stores the
// information whether ith vertex is reached
// at least once in the Breadth first search
bool visited[v];
// initially all vertices are unvisited
// so v[i] for all i is false
// and as no path is yet constructed
// dist[i] for all i set to infinity
for (int i = 0; i < v; i++) {
visited[i] = false;
dist[i] = INT_MAX;
pred[i] = -1;
}
// now source is first to be visited and
// distance from source to itself should be 0
visited[src] = true;
dist[src] = 0;
queue.push_back(src);
// standard BFS algorithm
while (!queue.empty()) {
int u = queue.front();
queue.pop_front();
for (int i = 0; i < adj[u].size(); i++) {
if (visited[adj[u][i]] == false) {
visited[adj[u][i]] = true;
dist[adj[u][i]] = dist[u] + 1;
pred[adj[u][i]] = u;
queue.push_back(adj[u][i]);
// We stop BFS when we find
// destination.
if (adj[u][i] == dest)
return true;
}
}
}
return false;
}
// utility function to print the shortest distance
// between source vertex and destination vertex
void printShortestDistance(vector<int> adj[], int s,
int dest, int v)
{
// predecessor[i] array stores predecessor of
// i and distance array stores distance of i
// from s
int pred[v], dist[v];
if (BFS(adj, s, dest, v, pred, dist) == false) {
cout << "Given source and destination"
<< " are not connected";
return;
}
// vector path stores the shortest path
vector<int> path;
int crawl = dest;
path.push_back(crawl);
while (pred[crawl] != -1) {
path.push_back(pred[crawl]);
crawl = pred[crawl];
}
// distance from source is in distance array
cout << "Shortest path length is : "
<< dist[dest];
// printing path from source to destination
cout << "\nPath is::\n";
for (int i = path.size() - 1; i >= 0; i--)
cout << path[i] << " ";
}
// Driver program to test above functions
int main()
{
// no. of vertices
int v = 8;
// array of vectors is used to store the graph
// in the form of an adjacency list
vector<int> adj[v];
// Creating graph given in the above diagram.
// add_edge function takes adjacency list, source
// and destination vertex as argument and forms
// an edge between them.
add_edge(adj, 0, 1);
add_edge(adj, 0, 3);
add_edge(adj, 1, 2);
add_edge(adj, 3, 4);
add_edge(adj, 3, 7);
add_edge(adj, 4, 5);
add_edge(adj, 4, 6);
add_edge(adj, 4, 7);
add_edge(adj, 5, 6);
add_edge(adj, 6, 7);
int source = 0, dest = 7;
printShortestDistance(adj, source, dest, v);
return 0;
}
Also in C++:
- Title
- c++ try
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- c++ pointers
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++
- Title
- how to make an array c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- sqrt in c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- double to string c++
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- cout value c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++