SlideShare a Scribd company logo
1 of 34
Download to read offline
10
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
Graph g:
myG.txt
6
8
0 5
2 4
2 3
1 2
0 1
3 4
3 5
0 2
V
E
Adjacency lists
adj[]
0
1
2
3
4
5
2
3
3
0
5
1
0
3
1
2
4
2
2
0
5
0
1
2
5
3 4
4
Challenge: build adjacency lists – Job done
11
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
add 0 to queue:
myG.txt
6
8
0 5
2 4
2 3
1 2
0 1
3 4
3 5
0 2
V
E
Adjacency lists
adj[]
0
1
2
3
4
5
2
3
3
0
5
1
0
3
1
2
4
2
2
0
5
0
1
2
5
3 4
4
12
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
add 0 to queue:
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 - -
3 - -
4 - -
5 - -
queue
0
13
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
0
2 1 5adj[0]
14
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
2 1 5adj[0]
15
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
2
2 1 5adj[0]
16
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
2
2 1 5adj[0]
17
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 - -
queue
1
2
2 1 5adj[0]
18
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 - -
queue
1
2
2 1 5adj[0]
19
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
2
2 1 5adj[0]
20
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
0 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
2
2 1 5adj[0]
21
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2:
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
2
22
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
adj[2] 0 1 3 4
23
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
adj[2] 0 1 3 4
24
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 - -
5 0 1
queue
3
5
1
adj[2] 0 1 3 4
25
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 - -
5 0 1
queue
3
5
1
adj[2] 0 1 3 4
26
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 - -
5 0 1
queue
4
3
5
1
adj[2] 0 1 3 4
27
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
2 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
1
adj[2] 0 1 3 4
28
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
1
29
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
30
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1: check 0, and check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
31
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1: check 0, and check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
32
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1: check 0, and check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
33
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
1 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
34
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 5: check 3 and check 0
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[5] 3 0
35
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
5 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
adj[5] 3 0
36
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 3: Check 5, Check 4, and Check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
adj[3] 5 4 2
37
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 3:
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
38
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 3: Check 5, Check 4, and Check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
adj[3] 5 4 2
39
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
3 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
adj[3] 5 4 2
40
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
41
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 4: Check 3 and Check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
adj[4] 3 2
42
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
4 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
43
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1

More Related Content

Recently uploaded

01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
AshwaniAnuragi1
 
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
A
 

Recently uploaded (20)

litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 

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...
 

Graph bfs

  • 1. 10 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. Graph g: myG.txt 6 8 0 5 2 4 2 3 1 2 0 1 3 4 3 5 0 2 V E Adjacency lists adj[] 0 1 2 3 4 5 2 3 3 0 5 1 0 3 1 2 4 2 2 0 5 0 1 2 5 3 4 4 Challenge: build adjacency lists – Job done
  • 2. 11 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. add 0 to queue: myG.txt 6 8 0 5 2 4 2 3 1 2 0 1 3 4 3 5 0 2 V E Adjacency lists adj[] 0 1 2 3 4 5 2 3 3 0 5 1 0 3 1 2 4 2 2 0 5 0 1 2 5 3 4 4
  • 3. 12 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. add 0 to queue: 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 - - 2 - - 3 - - 4 - - 5 - - queue 0
  • 4. 13 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 - - 2 0 1 3 - - 4 - - 5 - - queue 0 2 1 5adj[0]
  • 5. 14 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 - - 2 0 1 3 - - 4 - - 5 - - queue 2 1 5adj[0]
  • 6. 15 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 - - 2 0 1 3 - - 4 - - 5 - - queue 2 2 1 5adj[0]
  • 7. 16 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 - - 2 0 1 3 - - 4 - - 5 - - queue 2 2 1 5adj[0]
  • 8. 17 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 - - queue 1 2 2 1 5adj[0]
  • 9. 18 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 - - queue 1 2 2 1 5adj[0]
  • 10. 19 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 0: check2, check 1 and check 5 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 0 1 queue 5 1 2 2 1 5adj[0]
  • 11. 20 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. 0 done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 0 1 queue 5 1 2 2 1 5adj[0]
  • 12. 21 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 2: 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 0 1 queue 5 1 2
  • 13. 22 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 2: check 0, check 1, check 3 and check 4 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 0 1 queue 5 1 adj[2] 0 1 3 4
  • 14. 23 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 2: check 0, check 1, check 3 and check 4 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 - - 4 - - 5 0 1 queue 5 1 adj[2] 0 1 3 4
  • 15. 24 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 2: check 0, check 1, check 3 and check 4 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 - - 5 0 1 queue 3 5 1 adj[2] 0 1 3 4
  • 16. 25 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 2: check 0, check 1, check 3 and check 4 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 - - 5 0 1 queue 3 5 1 adj[2] 0 1 3 4
  • 17. 26 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 2: check 0, check 1, check 3 and check 4 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 - - 5 0 1 queue 4 3 5 1 adj[2] 0 1 3 4
  • 18. 27 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. 2 done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 1 adj[2] 0 1 3 4
  • 19. 28 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 1 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 1
  • 20. 29 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 1 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5
  • 21. 30 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 1: check 0, and check 2 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 adj[1] 0 2
  • 22. 31 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 1: check 0, and check 2 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 adj[1] 0 2
  • 23. 32 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 1: check 0, and check 2 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 adj[1] 0 2
  • 24. 33 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. 1 done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 adj[1] 0 2
  • 25. 34 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 5: check 3 and check 0 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 5 adj[5] 3 0
  • 26. 35 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. 5 done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 adj[5] 3 0
  • 27. 36 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 3: Check 5, Check 4, and Check 2 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3 adj[3] 5 4 2
  • 28. 37 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 3: 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 3
  • 29. 38 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 3: Check 5, Check 4, and Check 2 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 adj[3] 5 4 2
  • 30. 39 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. 3 done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4 adj[3] 5 4 2
  • 31. 40 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 4 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue 4
  • 32. 41 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. dequeue 4: Check 3 and Check 2 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue adj[4] 3 2
  • 33. 42 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. 4 done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1 queue
  • 34. 43 Breadth-first search demo Repeat until queue is empty: • Remove vertex v from queue. • Add to queue all unmarked vertices adjacent to v and mark them. done 0 1 2 5 3 4 v edgeTo[v] distTo[] 0 - 0 1 0 1 2 0 1 3 2 2 4 2 2 5 0 1