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:16] – mugabej | courses:cs211:winter2012:journals:jeanpaul:chapterthreesectioniii [2012/01/31 02:48] (current) – mugabej | ||
|---|---|---|---|
| Line 19: | Line 19: | ||
| * The sum of the degrees in a graph = **//2m//** | * The sum of the degrees in a graph = **//2m//** | ||
| + | |||
| + | === Queues and Stacks=== | ||
| + | |||
| + | * With A queue, we use the First in, First Out(FIFO) concept | ||
| + | * The new element is added to the end of the list | ||
| + | * For a stack, we use the Last in, First Out(LIFO) concept | ||
| + | * The new element is added at the front of the list | ||
| + | * In both implementations, | ||
| + | | ||
| + | |||
| + | * BFS: \\ | ||
| + | |||
| + | BFS(s):\\ | ||
| + | Discovered[v] = false, for all v\\ | ||
| + | Discovered[s] = true\\ | ||
| + | L[0] = {s}\\ | ||
| + | layer counter i = 0\\ | ||
| + | BFS tree T = {}\\ | ||
| + | while L[i] != {}\\ | ||
| + | >>>>> | ||
| + | >>>>> | ||
| + | >>>>>>>>> | ||
| + | >>>>>>>>> | ||
| + | >>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>> | ||
| + | >>>>>>>>>> | ||
| + | >>>>> | ||
| + | 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. | ||
