Author | Message |
---|---|
G190852562
Posts: 162
|
Posted 02:51 Dec 07, 2014 |
3. Consider an adjacency matrix representation of G = (V, E) Assume vertices are numbered 1, 2, … |V| The representation consists of a matrix AVxV aij = 1 if there is an edge from vertex i to vertex j; O( 1 ) I selected the 3rd option but it is incorrect. Can someone briefly explain this question? 4. Consider an adjacency list representation of an undirected graph G = (V, E). where V is the set of vertices and E is the set of edges. |E| aij = 1 if there is an edge from vertex i to vertex j; O(|V|^2) |
yxu22
Posts: 25
|
Posted 09:40 Dec 07, 2014 |
3. Answer is O(1) In an adjacency matrix, for example, A B C A 0 1 1 B 1 0 0 C 1 0 0 for example, the underscore 1 means there exists an edge between vertex A and vertex B. So if we want to determine if there is an edge (u,v) in E. We only need to check the element of matrix in u th row, v th column. It takes constant time. We only check something like return ( Matrix(u,v) == 1 )
4. Because it is an undirected graph. Each edge would be listed twice in an adjacency list. For example, there is an edge between vertex A and vertex B. In A's list, we would have a B In B's list, we would have a A They are the same edge. We store it twice. So O( 2|E| )
6. Answer is O( |V| ) According to the example above A B C A 0 1 1 B 1 0 0 C 1 0 0 For example, if we need to list all vertices adjacent to vertex A, we need to check all columns( or rows) of A. Then it takes O( |V| ) time. |
G190852562
Posts: 162
|
Posted 14:21 Dec 07, 2014 |
I understood the rest. However, can you give me an example of an adjacency list of a given graph? Adjacency List (or Array) representation of graphs array (or list) of vertices I know those are the properties. |
yxu22
Posts: 25
|
Posted 14:27 Dec 07, 2014 |
A B C A 0 1 1 B 1 0 0 C 1 0 0 Convert this adjacency matrix to adjacency list: List of vertices { A, B, C } List of A { B,C } List of B { A } List of C { A } |
G190852562
Posts: 162
|
Posted 14:32 Dec 07, 2014 |
It's all clear to me now, thanks! |