Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2012:journals:jeanpaul:chapterthreesectioniii [2012/01/31 02:39] – mugabej | courses:cs211:winter2012:journals:jeanpaul:chapterthreesectioniii [2012/01/31 02:48] (current) – mugabej | ||
|---|---|---|---|
| Line 27: | Line 27: | ||
| * The new element is added at the front of the list | * The new element is added at the front of the list | ||
| * In both implementations, | * In both implementations, | ||
| - | | + | |
| * BFS: \\ | * BFS: \\ | ||
| - | BFS(s): | + | BFS(s):\\ |
| - | Discovered[v] = false, for all v | + | Discovered[v] = false, for all v\\ |
| - | Discovered[s] = true | + | Discovered[s] = true\\ |
| - | L[0] = {s} | + | L[0] = {s}\\ |
| - | layer counter i = 0 | + | layer counter i = 0\\ |
| - | BFS tree T = {} | + | BFS tree T = {}\\ |
| - | while L[i] != {} | + | while L[i] != {}\\ |
| >>>>> | >>>>> | ||
| >>>>> | >>>>> | ||
| Line 49: | Line 49: | ||
| end while | end while | ||
| + | This implementation of the BFS takes O(m+n) if the graph is given by the adjacency list representation.\\ | ||
| + | |||
| + | * DFS:\\ | ||
| + | |||
| + | DFS(s):\\ | ||
| + | Initialize S to be a stack with one element s\\ | ||
| + | Explored[v] = false, for all v\\ | ||
| + | Parent[v] = 0, for all v \\ | ||
| + | DFS tree T = {} \\ | ||
| + | while S != {}\\ | ||
| + | >>>>>> | ||
| + | >>>>>> | ||
| + | >>>>>>>>>>> | ||
| + | >>>>>>>>>>> | ||
| + | >>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>> | ||
| + | >>>>>> | ||
| + | end while | ||
| + | |||
| + | This implementation of the DFS takes O(m+n) if the graph is given by the adjacency list representation.\\ | ||
| + | |||
| + | Thus when using a DFS or a BFS, one can easily implement graph traversals in linear time(O(m+n)).\\ | ||
| + | |||
| + | This section was interesting too, I give it an 8/10. | ||
