SlideShare a Scribd company logo
1 of 30
Download to read offline
14
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
Graph g
myG.txt
13
13
0 5
4 3
0 1
9 12
6 4
5 4
0 2
11 12
9 10
0 6
7 8
9 11
5 3
V
E
V-E lists
Adjacency lists
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
15
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 0: Which one first?
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 F -
4 F -
5 F -
6 F -
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
16
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
visit 0: check 6, check 2, check 1, and check 5
Adjacency lists
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 F -
4 F -
5 F -
6 F -
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
17
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
visit 6: check 0, check 4
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 F -
4 F -
5 F -
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
Adjacency lists
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
18
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
visit 4: check 5, check 6, check 3
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 F -
4 T 6
5 F -
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
19
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 F -
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
20
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 F -
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
21
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
22
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
visit 3: check 5, check 4
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
Adjacency lists
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
23
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 3: check 5, check 4
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
24
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 3: check 5, check 4
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
25
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 3: check 5, check 4
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
26
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
3 done:
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
27
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
done
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
28
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
done
29
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
done
30
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
visit 5: check 3, check 4, check 0
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
done
31
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
5 done
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
32
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 4: check 5, check 6, check 3
33
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 4: check 5, check 6, check 3
34
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 6: check 0, check 4
35
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 F -
2 F -
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 6: check 0, check 4
done 6
36
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
v marked[] edgeTo[v]
0 T -
1 F -
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 0: check 6, check 2, check 1, and check 5
Adjacency lists
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
37
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
v marked[] edgeTo[v]
0 T -
1 F -
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 2: check 0
Adjacency lists
adj[]
0
1
2
3
4
5
6
6
3
5
5
2
0
1 5
0
4
6 3
0 4
4 0
38
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 F -
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 2: check 0
39
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 F -
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 0: check 6, check 2, check 1, and check 5
40
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 T 0
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 1: check 0
41
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 T 0
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 0: check 6, check 2, check 1, and check 5
42
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 T 0
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
visit 0: check 6, check 2, check 1, and check 5
0 done
43
Depth-first search demo
To visit a vertex v:
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
0
1 2 6
5
3 4
7 8
9 10
11 12
v marked[] edgeTo[v]
0 T -
1 T 0
2 T 0
3 T 5
4 T 6
5 T 4
6 T 0
7 F -
8 F -
9 F -
10 F -
11 F -
12 F -
• found vertices reachable from 0 – dfs:0, 6, 4, 5, 3, 2, 1
• built a data structure edgeTo[v]

More Related Content

Recently uploaded

Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdfVinayVadlagattu
 
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.pdfAlexander Litvinenko
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
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, FunctionsVIEW
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelDrAjayKumarYadav4
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfKira Dess
 
Circuit Breakers for Engineering Students
Circuit Breakers for Engineering StudentsCircuit Breakers for Engineering Students
Circuit Breakers for Engineering Studentskannan348865
 
☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...
☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...
☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...mikehavy0
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
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.pptxMustafa Ahmed
 
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...Amil baba
 
Databricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdfDatabricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdfVinayVadlagattu
 
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 ...IJECEIAES
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfEr.Sonali Nasikkar
 
DBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptxDBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptxrajjais1221
 
Presentation on Slab, Beam, Column, and Foundation/Footing
Presentation on Slab,  Beam, Column, and Foundation/FootingPresentation on Slab,  Beam, Column, and Foundation/Footing
Presentation on Slab, Beam, Column, and Foundation/FootingEr. Suman Jyoti
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligencemahaffeycheryld
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentationsj9399037128
 
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...Christo Ananth
 

Recently uploaded (20)

Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
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
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
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
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
Circuit Breakers for Engineering Students
Circuit Breakers for Engineering StudentsCircuit Breakers for Engineering Students
Circuit Breakers for Engineering Students
 
☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...
☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...
☎️Looking for Abortion Pills? Contact +27791653574.. 💊💊Available in Gaborone ...
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
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
 
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...
 
Databricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdfDatabricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdf
 
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 ...
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
DBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptxDBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptx
 
Presentation on Slab, Beam, Column, and Foundation/Footing
Presentation on Slab,  Beam, Column, and Foundation/FootingPresentation on Slab,  Beam, Column, and Foundation/Footing
Presentation on Slab, Beam, Column, and Foundation/Footing
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
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...
 

Featured

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 HubspotMarius Sescu
 
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 ChatGPTExpeed Software
 
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 EngineeringsPixeldarts
 
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 HealthThinkNow
 
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.pdfmarketingartwork
 
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 2024Neil Kimberley
 
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)contently
 
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 2024Albert Qian
 
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 InsightsKurio // The Social Media Age(ncy)
 
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 2024Search Engine Journal
 
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 summarySpeakerHub
 
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 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 Tessa Mero
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
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 managementMindGenius
 
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...RachelPearson36
 

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 dfs-and-path

  • 1. 14 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 Graph g myG.txt 13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3 V E V-E lists Adjacency lists adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 2. 15 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 0: Which one first? v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 F - 4 F - 5 F - 6 F - 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 3. 16 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 visit 0: check 6, check 2, check 1, and check 5 Adjacency lists adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 F - 4 F - 5 F - 6 F - 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 4. 17 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 visit 6: check 0, check 4 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 F - 4 F - 5 F - 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - Adjacency lists adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 5. 18 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 visit 4: check 5, check 6, check 3 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 F - 4 T 6 5 F - 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 6. 19 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 F - 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 7. 20 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 F - 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 8. 21 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 9. 22 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 visit 3: check 5, check 4 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - Adjacency lists adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 10. 23 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 3: check 5, check 4 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 11. 24 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 3: check 5, check 4 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 12. 25 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 3: check 5, check 4 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 13. 26 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 3 done: v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 14. 27 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - done adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 15. 28 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - done
  • 16. 29 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - done
  • 17. 30 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 visit 5: check 3, check 4, check 0 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - done
  • 18. 31 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 5 done v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F -
  • 19. 32 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 4: check 5, check 6, check 3
  • 20. 33 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 4: check 5, check 6, check 3
  • 21. 34 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 6: check 0, check 4
  • 22. 35 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 F - 2 F - 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 6: check 0, check 4 done 6
  • 23. 36 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 v marked[] edgeTo[v] 0 T - 1 F - 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 0: check 6, check 2, check 1, and check 5 Adjacency lists adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 24. 37 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 v marked[] edgeTo[v] 0 T - 1 F - 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 2: check 0 Adjacency lists adj[] 0 1 2 3 4 5 6 6 3 5 5 2 0 1 5 0 4 6 3 0 4 4 0
  • 25. 38 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 F - 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 2: check 0
  • 26. 39 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 F - 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 0: check 6, check 2, check 1, and check 5
  • 27. 40 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 T 0 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 1: check 0
  • 28. 41 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 T 0 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 0: check 6, check 2, check 1, and check 5
  • 29. 42 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 T 0 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - visit 0: check 6, check 2, check 1, and check 5 0 done
  • 30. 43 Depth-first search demo To visit a vertex v: • Mark vertex v as visited. • Recursively visit all unmarked vertices adjacent to v. 0 1 2 6 5 3 4 7 8 9 10 11 12 v marked[] edgeTo[v] 0 T - 1 T 0 2 T 0 3 T 5 4 T 6 5 T 4 6 T 0 7 F - 8 F - 9 F - 10 F - 11 F - 12 F - • found vertices reachable from 0 – dfs:0, 6, 4, 5, 3, 2, 1 • built a data structure edgeTo[v]