SlideShare a Scribd company logo
CompSci 201
Recitation 14
Graph search exercises
April 20, 2015
Austin Lu (Head UTA)
Professor Azhar
A Social Network Graph
CompSci 201
• Imagine a social network graph with members as
vertices, identified by some integer ID
• Assume connections are mutual
• Is this graph directed or undirected?
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• Snarf the code for today
• How is the graph represented?
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• Which member has the most connections?
• How do you find out in code?
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• What are the connected components in the graph?
• How do we find them programmatically?
0
1
2
3
7
6
4
5
11
8
10
9
findConnectedComponents()
CompSci 201
• Complete the findConnectedComponents method,
which returns a Set<Set<Integer>> where each
element is a set of all members in a connected
component
• Why a set?
• Complete and use dfs(int start, Set<Integer> visited)
for this exercise
• Would breadth-first search also work?
Discussion
CompSci 201
• What should findConnectedComponents return on the
below graph (already created in main method)?
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• What should findConnectedComponents return on the
below graph (already created in main method)?
• {{0, 1, 2, 3, 4, 5, 6, 7, 11}, {8, 9, 10}}
0
1
2
3
7
6
4
5
11
8
10
9
Degrees of separation
CompSci 201
• The “degree of separation” between two vertices is the
minimum number of nodes separating them
• E.g. starting from Member 0, the degree of separation to
• Member 0 (itself) => 0
• Members 1, 2, 3, 7 => 1
0
1
2
3
7
6
4
5
11
8
10
9
Degrees of separation
CompSci 201
• E.g. starting from member 0, the degree of separation to
• Member 0 (itself) => 0
• Members 1, 2, 3, 7 => 1
• Members 4, 5, 6 => 2
• Member 11 => 3
• Members 8, 9, 10 (unreachable) => return -1
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• What should degreesOfSeparation(2, 4) return?
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• What should degreesOfSeparation(2, 4) return?
• 3
0
1
2
3
7
6
4
5
11
8
10
9
Degrees of separation
CompSci 201
• Complete the method to return the degree of separation
between two members, int degreesOfSeparation(int from, int to)
• Which graph search is convenient to search by distance?
Breadth-first search
• Use a queue of Member(int id, int distance) objects to
keep track of both member id and degree of separation/distance
• As soon as you visit a member with ID == to, you can return the
distance immediately
• Remember to return -1 if the member to is unreachable
BFS
CompSci 201
• Recall: BFS pseudocode
bfs(start vertex s) {
- mark s as visited
- initialize Queue with s
- while (Queue is not empty):
- remove first node of Q, call it curr
- for each neighbor n of curr:
-if n is unvisited
-mark n as visited
-add n to Queue
}
findNthDegreeConnections
CompSci 201
• How might we modify a BFS to find all Nth degree connections?
• Complete the method
Set<Integer> findNthDegreeConnections(int from, int n)
Discussion
CompSci 201
• What should findNthDegreeConnections(4, 3) return?
0
1
2
3
7
6
4
5
11
8
10
9
Discussion
CompSci 201
• What should findNthDegreeConnections(4, 3) return?
• {2, 3, 7, 11}
0
1
2
3
7
6
4
5
11
8
10
9
To Do:
• Submit the recitation quiz before Monday 4/27, 3pm
• 2 allowed submissions, last score counts
• Optional relevant reading:
• Six degrees of separation theory
• Kevin Bacon number
• Happy last recitation!
Good luck on finals
CompSci 201

More Related Content

Viewers also liked

CMII & Agile: There's a better way to do it - let's find it
CMII & Agile: There's a better way to do it - let's find itCMII & Agile: There's a better way to do it - let's find it
CMII & Agile: There's a better way to do it - let's find it
Willmund van Aarde - CMIIC, PRINCE2®
 
2015_833_Mannan_ppt
2015_833_Mannan_ppt2015_833_Mannan_ppt
2015_833_Mannan_ppt
Ziaul Mannan
 
Doordharsha
DoordharshaDoordharsha
Doordharsha
Shraddha Joshi
 
MOHSIN CV
MOHSIN CVMOHSIN CV
MOHSIN CV
Mohsin Tariq
 
Manuscript GNUR741 Diabetes group visit in a primary care
Manuscript GNUR741 Diabetes group visit in a primary careManuscript GNUR741 Diabetes group visit in a primary care
Manuscript GNUR741 Diabetes group visit in a primary care
Selma Mujezinovic
 
Japan’s Scenarios 2030 (Future Education Consortium)
Japan’s Scenarios 2030 (Future Education Consortium)Japan’s Scenarios 2030 (Future Education Consortium)
Japan’s Scenarios 2030 (Future Education Consortium)
未来教育会議
 

Viewers also liked (6)

CMII & Agile: There's a better way to do it - let's find it
CMII & Agile: There's a better way to do it - let's find itCMII & Agile: There's a better way to do it - let's find it
CMII & Agile: There's a better way to do it - let's find it
 
2015_833_Mannan_ppt
2015_833_Mannan_ppt2015_833_Mannan_ppt
2015_833_Mannan_ppt
 
Doordharsha
DoordharshaDoordharsha
Doordharsha
 
MOHSIN CV
MOHSIN CVMOHSIN CV
MOHSIN CV
 
Manuscript GNUR741 Diabetes group visit in a primary care
Manuscript GNUR741 Diabetes group visit in a primary careManuscript GNUR741 Diabetes group visit in a primary care
Manuscript GNUR741 Diabetes group visit in a primary care
 
Japan’s Scenarios 2030 (Future Education Consortium)
Japan’s Scenarios 2030 (Future Education Consortium)Japan’s Scenarios 2030 (Future Education Consortium)
Japan’s Scenarios 2030 (Future Education Consortium)
 

Recently uploaded

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
nitinpv4ai
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
National Information Standards Organization (NISO)
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 

Recently uploaded (20)

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 

Duke COMPSCI 201 Recitation 14 (Spring 2015)

  • 1. CompSci 201 Recitation 14 Graph search exercises April 20, 2015 Austin Lu (Head UTA) Professor Azhar
  • 2. A Social Network Graph CompSci 201 • Imagine a social network graph with members as vertices, identified by some integer ID • Assume connections are mutual • Is this graph directed or undirected? 0 1 2 3 7 6 4 5 11 8 10 9
  • 3. Discussion CompSci 201 • Snarf the code for today • How is the graph represented? 0 1 2 3 7 6 4 5 11 8 10 9
  • 4. Discussion CompSci 201 • Which member has the most connections? • How do you find out in code? 0 1 2 3 7 6 4 5 11 8 10 9
  • 5. Discussion CompSci 201 • What are the connected components in the graph? • How do we find them programmatically? 0 1 2 3 7 6 4 5 11 8 10 9
  • 6. findConnectedComponents() CompSci 201 • Complete the findConnectedComponents method, which returns a Set<Set<Integer>> where each element is a set of all members in a connected component • Why a set? • Complete and use dfs(int start, Set<Integer> visited) for this exercise • Would breadth-first search also work?
  • 7. Discussion CompSci 201 • What should findConnectedComponents return on the below graph (already created in main method)? 0 1 2 3 7 6 4 5 11 8 10 9
  • 8. Discussion CompSci 201 • What should findConnectedComponents return on the below graph (already created in main method)? • {{0, 1, 2, 3, 4, 5, 6, 7, 11}, {8, 9, 10}} 0 1 2 3 7 6 4 5 11 8 10 9
  • 9. Degrees of separation CompSci 201 • The “degree of separation” between two vertices is the minimum number of nodes separating them • E.g. starting from Member 0, the degree of separation to • Member 0 (itself) => 0 • Members 1, 2, 3, 7 => 1 0 1 2 3 7 6 4 5 11 8 10 9
  • 10. Degrees of separation CompSci 201 • E.g. starting from member 0, the degree of separation to • Member 0 (itself) => 0 • Members 1, 2, 3, 7 => 1 • Members 4, 5, 6 => 2 • Member 11 => 3 • Members 8, 9, 10 (unreachable) => return -1 0 1 2 3 7 6 4 5 11 8 10 9
  • 11. Discussion CompSci 201 • What should degreesOfSeparation(2, 4) return? 0 1 2 3 7 6 4 5 11 8 10 9
  • 12. Discussion CompSci 201 • What should degreesOfSeparation(2, 4) return? • 3 0 1 2 3 7 6 4 5 11 8 10 9
  • 13. Degrees of separation CompSci 201 • Complete the method to return the degree of separation between two members, int degreesOfSeparation(int from, int to) • Which graph search is convenient to search by distance? Breadth-first search • Use a queue of Member(int id, int distance) objects to keep track of both member id and degree of separation/distance • As soon as you visit a member with ID == to, you can return the distance immediately • Remember to return -1 if the member to is unreachable
  • 14. BFS CompSci 201 • Recall: BFS pseudocode bfs(start vertex s) { - mark s as visited - initialize Queue with s - while (Queue is not empty): - remove first node of Q, call it curr - for each neighbor n of curr: -if n is unvisited -mark n as visited -add n to Queue }
  • 15. findNthDegreeConnections CompSci 201 • How might we modify a BFS to find all Nth degree connections? • Complete the method Set<Integer> findNthDegreeConnections(int from, int n)
  • 16. Discussion CompSci 201 • What should findNthDegreeConnections(4, 3) return? 0 1 2 3 7 6 4 5 11 8 10 9
  • 17. Discussion CompSci 201 • What should findNthDegreeConnections(4, 3) return? • {2, 3, 7, 11} 0 1 2 3 7 6 4 5 11 8 10 9
  • 18. To Do: • Submit the recitation quiz before Monday 4/27, 3pm • 2 allowed submissions, last score counts • Optional relevant reading: • Six degrees of separation theory • Kevin Bacon number • Happy last recitation! Good luck on finals CompSci 201

Editor's Notes

  1. The key that has the value with the largest size, here it’s member 0
  2. Use Set because each member/vertex can only be a part of one connected component. BFS also works to find connected components.