Download Analog and digital C - GRAPH REPRESENTATION - ADJACENCY MATRIX, LIST, DFS, NFS and more Study notes Digital & Analog Electronics in PDF only on Docsity! GRAPH REPRESENTATION
& Adjacency Matrix
# Adjacency Lists
Ā© DFS
Ā® NFS
Graph Representations
* Adjacency Matrix
* Adjacency Lists
Adjacency Matrices WHAT IS THE ADJACENCY MATRIX FOR THE GRAPH GIVEN? n1 n5 n2 n3 n4 n6 n7 e1 e2 e3 e4 e5 e6 e7 Adjacency Matrices n1 n5 n2 n3 n4 n6 n7 e1 e2 e3 e4 e5 e6 e7 Adj. Matrix ā¢ WHAT IS THE ADJACENCY MATRIX FOR THE GRAPH GIVEN? 2 3 0 1 4 5 Adjacency Lists Representation ā¢ A graph of n nodes is represented by a one-dimensional array L of linked lists, where ā L[i] is the linked list containing all the nodes adjacent from node i. ā The nodes in the list L[i] are in no particular order Example of Linked Representation L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 John 2 Joe 3 Mary 0 Helen 1 Tom 4 Paul 5 Pros and Cons of Adjacency Lists ā¢ Pros: ā Saves on space (memory): the representation takes as many memory words as there are nodes and edge. ā¢ Cons: ā It can take up to O(n) time to determine if a pair of nodes (i,j) is an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i]. Adjacency matrix vs Adjacency list Adjacency matrix ļ® Always requires n2 space ļµ This can waste a lot of space if the number of edges are sparse ļ® Can quickly find if an edge exist Adjacent List ļ® More compact than adjacency matrix ļ® Requires more for finding if edges exist Graph Traversal ā¢ Problem: Search for a certain node or traverse all nodes in the graph ā¢ Depth First Search ā Once a possible path is found, continue the search until the end of the path ā¢ Breadth First Search ā Start several paths at a time, and advance in each one step at a time Depth-First Search ā¢ DFS follows the following rules: 1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1. Applications: Finding a Path ā¢ Find path from source vertex s to destination vertex d ā¢ Use graph search starting at s and terminating as soon as we reach d ā Need to remember edges traversed DFS E F G B CD A start destination A DFS on A A DFS on BB A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on GG found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process Breadth-First Search ā¢ BFS follows the following rules: 1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1.