 ## ← BFS Try 1 - An Example - Intro to Parallel Programming

• 3 Followers
• 37 Lines

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=I0MQWvWh8HM" data-team="udacity"></div> ``` 2 Languages

• English [en] original
• Chinese, Simplified [zh-cn]

Showing Revision 5 created 05/25/2016 by Udacity Robot.

1. So here's a simple graph.
2. We're going to start here with node number 2,
3. and we're going to set the depth there equal to 0.
4. And our goal is to find these depths for each of these vertices.
5. Originally, none of these are set.
6. We're just going to say that's the value for hasn't visited yet.
7. And we're going to begin by setting vertex number 2 to the value 0.
8. So we note that the furthest vertex away from node 2 is two hops away.
9. So the maximum depth here is 2, and we should expect that we should iterate twice.
10. So we're going to begin the algorithm by looking at all of these edges in parallel.
11. And what we're looking for is a pattern
12. where one of the vertices of these edges is going to have a depth value set,
13. and the other one does not.
14. And we're going to find that 3 edges have this particular quality.
15. So one of them is going to be the edge between 1 and 2.
16. We'll check and see.
17. The depth at vertex 2 is 0 and vertex 1 has not been visited yet.
18. So we'll set it equal to 0 plus 1, which is 1.
19. We'll do the same thing for this edge here between 2 and 3, okay?
20. We visited 2; its value was 0.
21. We haven't visited 3, so we set its value to be 1.
22. And this edge here, 2 and 6.
23. Now we complete the first iteration.
24. We visited every single edge
25. and run 6 threads in parallel to make this particular determination.
26. Now we begin our second iteration.
27. So now, again, what we're looking for is an edge
28. where the vertex value of one end of the edge has been set
29. and the other one has not yet been visited.
30. So we're going to find that's true for the other 3 edges.
31. So the edge between 0 and 1, we find that the depth at vertex number 1 is 1.
32. We find that vertex 0 has not yet been visited.
33. So we set its depth to 1 plus 1 equals 2.
34. And the same is going to be true for the edges between 3 and 4
35. and the edge between 5 and 6, okay?
36. Now we filled in a value for all of our vertices here,
37. and we're thus complete.