Directed Vs Undirected Graph
Directed Vs Undirected Graph
A directed graph
G = (V, E ) with vertex set V = 0, 1, 2, 3, 4, 5, 6 and edge set E = (0, 2), (0, 4), (0, 5), (1, 0), (2, 1), (2, 5), (3, 1), (3, 6), (4, 0), (4, 5), (6, 3), (6, 5) .
An undirected graph
1 2 0 10 11 4 9 12 8 6 7 5
Examples (contd)
Airline route maps. Vertices represent airports, and there is an edge from vertex A to vertex B if there is a direct ight from the airport represented by A to the airport represented by B .
Examples (contd)
Electrical Circuits. Vertices represent diodes, transistors, capacitors, switches, etc., and edges represent wires connecting them.
Examples (contd)
Computer Networks. Vertices represent computers and edges represent network connections (cables) between them. The World Wide Web. Vertices represent webpages, and edges represent hyperlinks.
Examples (contd)
Flowcharts. Vertices represent boxes and edges represent arrows. Conict graphs in scheduling problems. Example: Assignment of time slots to exams. No overlapping time slots for exams taken by the same students. Modeled by graph whose vertices represent the exams, with an edge between two vertices if there is a student who has to take both exams.
10
Examples (contd)
Molecules. Vertices are atoms, edges are bonds between them.
H H C H
H C H O H
11
Examples (contd)
Binary Relations in Mathematics. For example, the is a proper divisor relation on the integers.
8
10
12
Let G = (V, E ) be a graph with n vertices. Vertices of G numbered 0, . . . , n 1. The adjacency matrix of G is the n n matrix A = (aij )0i,j n1 with aij = 1 0 if there is an edge from vertex i to vertex j otherwise.
13
Example
0 1 0 0 1 0 0
0 0 1 1 0 0 0
1 0 0 0 0 0 0
0 0 0 0 0 0 1
1 0 0 0 0 0 0
1 0 1 0 1 0 1
0 0 0 1 0 0 0
14
0 1 2
2 0 1 1 0
5 6 5
3 4 5
15
16
17
Graph traversals
A traversal is a strategy for visiting all vertices of a graph. BFS = breadth-rst search DFS = depth-rst search General strategy: 1. Let v be an arbitrary vertex 2. Visit all vertices reachable from v 3. If there are vertices that have not been visited, let v be such a vertex and go back to (2)
18
BFS
Visit all vertices reachable from v in the following order: v all neighbours of v all neighbours of neighbours of v that have not been visited yet all neighbours of neighbours of neighbours of v that have not been visited yet etc.
19
BFS (contd)
Algorithm bfs(G)
1. 2. 3. 4. 5.
Initialise Boolean array visited by setting all entries to false Initialise Queue Q for all v V do if visited [v ] = false then bfsFromVertex(G, v )
20
BFS (contd)
Algorithm bfsFromVertex(G, v )
1. 2. 3. 4. 5. 6. 7. 8.
visited [v ] = true Q.enqueue(v ) while not Q.isEmpty() do v Q.dequeue() for all w adjacent to v do if visited [w] = false then visited [w] = true Q.enqueue(w)
21
DFS
Visit all vertices reachable from v in the following order: v some neighbor w of v that has not been visited yet some neighbor x of w that has not been visited yet etc., until the current vertex has no neighbour that has not been visited yet Backtrack to the rst vertex that has a yet unvisited neighbour v . Continue with v , a neighbour, a neighbour of the neighbour, etc., backtrack, etc.
22
DFS (contd)
Algorithm dfs(G)
1. 2. 3. 4. 5.
Initialise Boolean array visited by setting all entries to false Initialise Stack S for all v V do if visited [v ] = false then dfsFromVertex(G, v )
23
DFS (contd)
Algorithm dfsFromVertex(G, v )
1. 2. 3. 4. 5. 6. 7.
S.push(v ) while not S.isEmpty() do v S.pop() if visited [v ] = false then visited [v ] = true for all w adjacent to v do S.push(w)