midterm2key - 1 1(7 pts Using adjacency matrix M for i = 1...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1). (7 pts) Using adjacency matrix M for( i = 1; i <= |V|; i++) for( j = 1; j <= |V|; j++) for(k = 1; k <= |V|; k++) { if( i != j != k && M[i,j] == 1 && M[j,k] == 1 && M[k,i] == 1) return TRUE; } Complexity: nested loop will be executed in O(|V| 3 ) 2). (13 pts) Using adjacency list For every edge e = (i, j), check the adjacency lists of vertex i and vertex j to see if the two lists have a common vertex k, i.e. vertex k appears both in the adjacency lists of i and j. Implementation detail: We maintain a bit vector BV of size |V|. When checking adjacency lists of vertex i, we set BV[x] = 1 if vertex x appears in the list, BV[x] = 0 otherwise. Then we check adjacency lists of vertex j to see if there is any vertex y that BV[y] = 1, if so, y is the common vertex of the two lists. Complexity: for every edge, the processing time is at most 2*|V-1| as the max degree of any vertex is |V-1|, so the total time complexity is O(|V|*|E|) Common mistake: DFS on G, when encountering a back edge, check if the cycle is of length 3. counter example:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 3

midterm2key - 1 1(7 pts Using adjacency matrix M for i = 1...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online