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:00] – mugabej | courses:cs211:winter2012:journals:jeanpaul:chapterthreesectioniii [2012/01/31 02:48] (current) – mugabej | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| A graph can be represented as either an //adjacency matrix// or an //adjacency list//. with a graph G = (V,E), the number of nodes |V| is denoted as **//n//** and the number of edges |E| is denoted as **//m//**. When discussing the representations of graphs, our aim is to get a representation that allows us to implement the graph in a polynomial running time. The number of edges //m// is always less than or equal to n< | A graph can be represented as either an //adjacency matrix// or an //adjacency list//. with a graph G = (V,E), the number of nodes |V| is denoted as **//n//** and the number of edges |E| is denoted as **//m//**. When discussing the representations of graphs, our aim is to get a representation that allows us to implement the graph in a polynomial running time. The number of edges //m// is always less than or equal to n< | ||
| + | \\ | ||
| + | \\ | ||
| **Some Definitions**: | **Some Definitions**: | ||
| * //Adjacency matrix//: it's an //n X n// matrix A where A[u,v] is equal to 1 if the graph contains the edge (u,v), and 0 otherwise. This representation allows us to represent graphs in O(1) time if a the edge (u,v) is in the graph. | * //Adjacency matrix//: it's an //n X n// matrix A where A[u,v] is equal to 1 if the graph contains the edge (u,v), and 0 otherwise. This representation allows us to represent graphs in O(1) time if a the edge (u,v) is in the graph. | ||
| Line 7: | Line 9: | ||
| * The representation takes Θ(n< | * The representation takes Θ(n< | ||
| * Many graph algorithms need to examine all edges incident to a given node v. With the adjacency matrix, this operation takes Θ(n) time. More efficient algorithms exist solve this problem. | * Many graph algorithms need to examine all edges incident to a given node v. With the adjacency matrix, this operation takes Θ(n) time. More efficient algorithms exist solve this problem. | ||
| + | * //Adjacency list//: With this representation, | ||
| + | * With this representation we have an array Adj, where Adj[v] contains a record of all nodes adjacent to the node v. | ||
| + | * Comparison of the Adjacency list and the Adjacency matrix: | ||
| + | * Since an Adjacency matrix requires an n X n matrix, it is O(n< | ||
| + | * An Adjacency list takes O(m+n) space. | ||
| + | * In the Adjacency list, it takes O(// | ||
| + | * The degree // | ||
| + | * The length of the list at Adj[v] is // | ||
| + | * 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. | ||
