Download presentation
Presentation is loading. Please wait.
1
Last lesson Graphs Today Graphs (Implementation, Traversal)
2
Implementation There are different approaches Adjacency list Each node has a list of outgoing edges Perhaps also list of incoming edges nodes stored as array, or list, or symbol table (with label as key) Adjacency matrix Two-dimensional array A Entry A[i][j] is 1 (or cost) if there is an edge from v i to v j Often also keep symbol table to map label to index of node Incidence matrix Two-dimensional array I Entry I[i][j] is 1 (or cost) if node v i is connected with edge e j
3
Adjacency List NodesAdjacencies ab, e ba, c, e cb, d dc, e ea, b, d b c d e a
4
Adjacency List Adjacency list representation of the graph shown above. The elements in list i represent nodes adjacent to i and the cost of the connecting edge.
5
Adjacency and Incidence Matrix Adjacency Matrix Incidence Matrix abcde a01001 b10101 c01010 d00101 e11010 123456 a110000 b011010 c000011 d000101 e101100 b c d e a 1 2 3 4 5 6
6
Graph Data Structure: Basic Operations Insert node Insert edge from one node to another Delete edge Delete node Must also delete adjacent edges Given edge, find start and finish node Go though all nodes Given node, go through edges out of it Given node, go through edges into it
7
Complex Operations Find whether there is a path from one node to another If edges have costs, find least cost path Find all nodes reachable from one node Find whether there is a cycle Find a sequence of nodes that is consistent with all directed edges No edge goes backwards in the sequence
8
Graph Traversal Do something with each node “visit” the node Different algorithms visit in different orders Can be used as basis for many code cliches E.g. count the nodes E.g. find maximum/minimum/sum of a quantity Also the basis for important graph calculations
9
Overview Keep a collection of nodes that are waiting to be visited Start at one node s When you visit node v Consider all nodes that can be reached in one step from v They are the finish for each edge starting at v Any such node can be added to the waiting collection Unless its already there Vital choice: how to choose next node from the waiting collection? Breadth-first Search (BFS) Depth-first Search (DFS) Topological Sort
10
Breadth-first Search Waiting nodes kept in queue Initially, insert one node Dequeue a node Visit it Enqueue any adjacent node that is not already visited or waiting Repeat till queue is empty This will visit all nodes which can be reached from initial one Repeat with a new (as yet unvisited) initial node Do BFS from each Until all nodes have been visited
11
BFS Example rstu vwxy
12
0 rstu vwxy s Q:
13
BFS Example 1 0 1 rstu vwxy w Q: r
14
BFS Example 1 0 1 2 2 rstu vwxy r Q: tx
15
BFS Example 1 2 0 1 2 2 rstu vwxy Q: txv
16
BFS Example 1 2 0 1 2 2 3 rstu vwxy Q: xvu
17
BFS Example 1 2 0 1 2 2 3 3 rstu vwxy Q: vuy
18
BFS Example 1 2 0 1 2 2 3 3 rstu vwxy Q: uy
19
BFS Example 1 2 0 1 2 2 3 3 rstu vwxy Q: y
20
BFS Example 1 2 0 1 2 2 3 3 rstu vwxy Q: Ø
21
Implementation Local variables for the traversal routine Queue of waiting nodes Currently visited node Need to find all nodes adjacent to it Call routine of Graph class Efficient with Adjacency List representation Way to test if a node has been visited or is waiting Symbol table as Set Or, keep extra boolean in each node object Run-time cost O(E+V) with adjacency list representation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.