SlideShare a Scribd company logo
1 of 18
FIND ALL EDGES IN A
GRAPH WHICH BELONG
TO ANY SHORTEST PATH
FROM SOURCE TO
Concepts used – BFS only
QUESTION
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
1 56 7
12 13
Source Destination
Graph containing only those
edges belonging to shortest
path from 1 to 5
From To
5 7
5 13
7 6
13 12
6 1
12 1
Solution Edges
List
Expected Time Complexity –
O(V+E)
APPROACH
1
2 3 4
56
8 9 10 11
12 13
7
Source Destination
Shortest Path are from 1 to 5
are –
1 – 6 – 7 – 5
1 – 12 – 13 – 5
APPROACH
1
2 3 4
56
8 9 10 11
12 13
7
Source Destination
Steps –
1. Apply BFS from Source node.
2. Store edges which are part of
shortest distance to that node.
APPROACH
1
2
6
8
12
Source
Child Paren
t
2 1
6 1
8 1
12 1
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
APPROACH
1
2 3
6 7
8 9
12 13
Source
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
APPROACH
1
2 3 4
6 7
8 9 10
12 13
Source
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
4 3
10 9
APPROACH
1
2 3 4
56 7
8 9 10
12 13
Source Destination
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7
APPROACH
1
2 3 4
56 7
8 9 10
12 13
Source Destination
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
This edge also gives
Shortest distance to 5 =
3 using path 1 – 12 – 13
– 5
So include this edge too
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
Inclusion of edge from
4 to 5 gives distance
from 1 to 5 = 4 which is
larger than shortest
distance to 5 (equal to
3). So we will not
include it in our parent-
child table.
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
Inclusion of edge from
11 to 5 gives distance
from 1 to 5 = 5 which is
larger than shortest
distance to 5 (equal to
3). So we will not
include it in our parent-
child table.
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
Next Step –
Apply BFS/DFS from Destination using
parent child table and store all the
edges which come while traversing the
graph.
Do not continue the BFS/DFS to the
edges of source node. So stop at
source node.
Stop the BFS at when is
Destination. Do not
continue the BFS as it
will include unnecessary
edges.
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
57
13
Destination
From To
5 7
5 13
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
56 7
12 13
Destination
From To
5 7
5 13
7 6
13 12
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
1 56 7
12 13
Source Destination
From To
5 7
5 13
7 6
13 12
6 1
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
APPROACH
1 56 7
12 13
Source Destination
Do not explore to
the edges of
Source Node.
From To
5 7
5 13
7 6
13 12
6 1
12 1
Solution Edges
List
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
CODE FOR BFS
void bfs1(int source,int destintion)
{
for(int i=0;i<100000;i++)
dis[i]=inf
dis[source]=0;
queue<pair<int,int> > q;// first number is node, second number is distance
q.push(mp(source,0));
while(!q.empty())
{
int node = q.front().first;
int d = q.front().second;
q.pop();
if(node==destination)
break;
for(int i=0;i<v[node].size();i++)
{
int neighbour = v[node][i];
if(d+1<dis[neighbour])
{
dis[neighbour] = d+1;
q.push(mp(neighbour,d+1));
par[neighbour].pb(node)
}
else if(d+1==dis[neighbour])
{
par[neighbour].pb(node);
}
}
}
Global Variables Declared -
#define inf INT_MAX
#define pb push_back
#define mp make_pair
vector<int> v[100000];
vector<int> par[100000];
int dis[100000];
vector<pair<int,int> > sol_edges;
void bfs2(int source,int destination)
{
int vis[100000]={0};
queue<int> q;
q.push(destination);
while(!q.empty())
{
int node = q.front();
q.pop();
if(vis[node]!=0)
continue;
if(node==source)
break;
for(int i=0;i<par[node].size();i++)
{
int neighbour = par[node][i];
if(vis[neighbour]!=0)
continue;
sol_edges.pb(mp(node,neighbour));
q.push(neighbour);
}
}
return;
Global Variables Declared -
#define inf INT_MAX
#define pb push_back
#define mp make_pair
vector<int> v[100000];
vector<int> par[100000];
int dis[100000];
vector<pair<int,int> >
sol_edges;
CODE FOR BFS - 2

More Related Content

Recently uploaded

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Find all edges in a graph which belong

  • 1. FIND ALL EDGES IN A GRAPH WHICH BELONG TO ANY SHORTEST PATH FROM SOURCE TO Concepts used – BFS only
  • 2. QUESTION 1 2 3 4 56 7 8 9 10 11 12 13 Source Destination 1 56 7 12 13 Source Destination Graph containing only those edges belonging to shortest path from 1 to 5 From To 5 7 5 13 7 6 13 12 6 1 12 1 Solution Edges List Expected Time Complexity – O(V+E)
  • 3. APPROACH 1 2 3 4 56 8 9 10 11 12 13 7 Source Destination Shortest Path are from 1 to 5 are – 1 – 6 – 7 – 5 1 – 12 – 13 – 5
  • 4. APPROACH 1 2 3 4 56 8 9 10 11 12 13 7 Source Destination Steps – 1. Apply BFS from Source node. 2. Store edges which are part of shortest distance to that node.
  • 5. APPROACH 1 2 6 8 12 Source Child Paren t 2 1 6 1 8 1 12 1 Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1
  • 6. APPROACH 1 2 3 6 7 8 9 12 13 Source Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1 3 2 7 2 9 2 13 2 Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12
  • 7. APPROACH 1 2 3 4 6 7 8 9 10 12 13 Source Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1 3 2 7 2 9 2 13 2 10 3 4 3 Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 4 3 10 9
  • 8. APPROACH 1 2 3 4 56 7 8 9 10 12 13 Source Destination Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1 3 2 7 2 9 2 13 2 10 3 4 3 5 3 Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7
  • 9. APPROACH 1 2 3 4 56 7 8 9 10 12 13 Source Destination Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1 3 2 7 2 9 2 13 2 10 3 4 3 5 3 This edge also gives Shortest distance to 5 = 3 using path 1 – 12 – 13 – 5 So include this edge too Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 10. APPROACH Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1 3 2 7 2 9 2 13 2 10 3 4 3 5 3 1 2 3 4 56 7 8 9 10 11 12 13 Source Destination Inclusion of edge from 4 to 5 gives distance from 1 to 5 = 4 which is larger than shortest distance to 5 (equal to 3). So we will not include it in our parent- child table. Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 11. APPROACH Node Shortest Distance From Source 1 0 2 1 6 1 8 1 12 1 3 2 7 2 9 2 13 2 10 3 4 3 5 3 1 2 3 4 56 7 8 9 10 11 12 13 Source Destination Inclusion of edge from 11 to 5 gives distance from 1 to 5 = 5 which is larger than shortest distance to 5 (equal to 3). So we will not include it in our parent- child table. Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 12. APPROACH 1 2 3 4 56 7 8 9 10 11 12 13 Source Destination Next Step – Apply BFS/DFS from Destination using parent child table and store all the edges which come while traversing the graph. Do not continue the BFS/DFS to the edges of source node. So stop at source node. Stop the BFS at when is Destination. Do not continue the BFS as it will include unnecessary edges. Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 13. APPROACH 57 13 Destination From To 5 7 5 13 Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 14. APPROACH 56 7 12 13 Destination From To 5 7 5 13 7 6 13 12 Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 15. APPROACH 1 56 7 12 13 Source Destination From To 5 7 5 13 7 6 13 12 6 1 Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 16. APPROACH 1 56 7 12 13 Source Destination Do not explore to the edges of Source Node. From To 5 7 5 13 7 6 13 12 6 1 12 1 Solution Edges List Child Paren t 2 1 6 1 8 1 12 1 3 2 7 6 9 8 13 12 5 7, 13
  • 17. CODE FOR BFS void bfs1(int source,int destintion) { for(int i=0;i<100000;i++) dis[i]=inf dis[source]=0; queue<pair<int,int> > q;// first number is node, second number is distance q.push(mp(source,0)); while(!q.empty()) { int node = q.front().first; int d = q.front().second; q.pop(); if(node==destination) break; for(int i=0;i<v[node].size();i++) { int neighbour = v[node][i]; if(d+1<dis[neighbour]) { dis[neighbour] = d+1; q.push(mp(neighbour,d+1)); par[neighbour].pb(node) } else if(d+1==dis[neighbour]) { par[neighbour].pb(node); } } } Global Variables Declared - #define inf INT_MAX #define pb push_back #define mp make_pair vector<int> v[100000]; vector<int> par[100000]; int dis[100000]; vector<pair<int,int> > sol_edges;
  • 18. void bfs2(int source,int destination) { int vis[100000]={0}; queue<int> q; q.push(destination); while(!q.empty()) { int node = q.front(); q.pop(); if(vis[node]!=0) continue; if(node==source) break; for(int i=0;i<par[node].size();i++) { int neighbour = par[node][i]; if(vis[neighbour]!=0) continue; sol_edges.pb(mp(node,neighbour)); q.push(neighbour); } } return; Global Variables Declared - #define inf INT_MAX #define pb push_back #define mp make_pair vector<int> v[100000]; vector<int> par[100000]; int dis[100000]; vector<pair<int,int> > sol_edges; CODE FOR BFS - 2