Uploaded by Himanshu Arora

Graphs

advertisement
Graphs
Introduction
A data structure that consists of a set of nodes (vertices) and a set of edges that
relate the nodes to each other
Formal Definition:
A graph G is a collection of two sets V and E represented as G(V, E) where,
V – Collection of vertices represented as V(G) = V0, V1, V2, V3 ……… Vn-1
E – Collection of edges represented as E(E) = E1, E2, E3 ……… En
Each edge is a tuple (Vx, Vy), where Vx, Vy in V
Team 1116_002, Data Structures
Types of graph
1. Undirected Graph
-
A graph which has unordered pair of vertices
-
If there is any edge between Vx to Vy then it can be presented (Vx, Vy ) or (Vy, Vx )
Let a graph G(V, E) where,
1
V(G) = {1,2,3,4,5}
2
4
E(G) = {(1,2), (1,3), (1,5), (2,5), (3,4), (4,5)}
3
5
Team 1116_002, Data Structures
Types of graph (Cont…)
2.
Directed Graph
-
A graph which has ordered pair of vertices
-
If there is any edge between Vx to Vy then it can be presented only (Vx, Vy )
Let a graph G(V, E) where,
1
V(G) = {1,2,3,4,5}
2
E(G) = {(1,2), (1,5),(2,1), (2,4), (3,1), (3,4), (5,4)}
4
3
5
Team 1116_002, Data Structures
Terminology
•
•
•
•
•
•
•
•
•
•
•
•
•
Weighted Graph
Adjacent nodes
Incidence
Path
Length of path
Closed path
Cycle (Loop)
Cyclic graph
Acyclic graph
Dag – directed acyclic graph
Degree – in degree, out degree
Source
Sink
Team 1116_002, Data Structures
Terminology (Cont…)
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Pendent node
Reachable node
Isolated node
Successor
Predecessor
Connected graph
Strongly connected graph
Weakly connected graph
Maximum number of edges
Complete graph
Planar graph
Articulation point
Bridge
Bi-connected graph
Tree
Team 1116_002, Data Structures
Representation of graph
To represent graph in computer memory –
1. Adjacency matrix
2. Adjacency list
Team 1116_002, Data Structures
Adjacency matrix
Graph is represented using matrix
This is a sequential representation
Matrix keeps the information about adjacent nodes for each node
If node is adjacent then put 1 otherwise put 0
i.e.
Mat[i][j] = 1 if there is an edge from node i to j
= 0 if there is no edge from node i to j
If graph is weighted graph then adjacent matrix will contain following –
Mat[i][j] = weight on edge
=0
if there is an edge from node i to j
if there is no edge from node i to j
Team 1116_002, Data Structures
Adjacency matrix (cont…)
A
C
D
B
E
A
C
D
B
E
A
B
C
D
E
A
B
C
D
E
A
B
C
D
E
0
0
1
0
1
1
0
1
0
0
0
0
0
0
1
0
0
1
0
0
0
1
0
1
0
A
B
C
D
E
0
1
1
1
0
1
0
0
1
0
1
0
0
1
1
1
1
1
0
0
0
0
1
0
0
Team 1116_002, Data Structures
Adjacency matrix (cont…)
3
A
C
2
D
8
1
4
B
3
7
5
E
A
B
C
D
E
A
B
C
D
E
0
0
3
0
3
1
0
4
0
0
0
0
0
0
8
0
0
2
0
0
0
7
0
5
0
Memory required to store graph G(V,E) using adjacency matrix is O(V2)
Team 1116_002, Data Structures
Adjacency List
Graph is represented using linked list
List keeps the information about edges
An array of space V contains the information about vertices
If graph is directed or undirected then adjacent list structure –
struct GNode{
int data;
struct GNode *link;
};
GNode *adj[V];
where, V is number of vertices.
If graph is weighted graph then adjacent list structure –
struct GNode{
int data; int weight;
struct GNode *link;
};
GNode *adj[V];
Team 1116_002, Data Structures
Adjacency List (cont…)
A
C
D
B
E
A
E
C
B
A
C
C
D
E
E
C
D
Team 1116_002, Data Structures
B
Adjacency List (cont…)
3
A
C
2
D
8
1
4
B
3
7
5
E
A
E
3
C 3
B
A 1
C 4
C
D
E
E
8
C 2
D 5
Less memory requirement than adjacency matrix
If graph is large then adjacency matrix is preferred
Team 1116_002, Data Structures
B 7
Operations on graph
1. Searching
2. Insertion
3. Deletion
4. Traversing
5. Sorting
Team 1116_002, Data Structures
Adjacency list structure
C Code for structures of nodes and header nodes –
struct Node{
int AINFO;
struct Node *Next;
};
struct Hnode{
int INFO;
struct Hnode *LINK;
struct Node *ALINK;
};
Hnode *Graph_start;
Team 1116_002, Data Structures
Searching
Node Searching –
Algorithm Node_search(Graph_start, Item): This algorithm finds the location of
Item(vertex) in Graph pointed by Graph_start. It returns the LOC (location of vertex) if
found.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
LOC = NULL
Hnode Temp
Temp = Graph_start
While Temp != NULL
If Temp->INFO = = Item
Set LOC = Temp
Return LOC
Else
Temp = Temp->LINK
Return LOC
Team 1116_002, Data Structures
Searching (Cont…)
Edge Searching –
Algorithm Edge_search(Graph_start, src, dst): This algorithm finds the location of
destination (dst) in Graph pointed by Graph_start. It returns the LOC (location of
destination vertex) if found.
1. LOC = NULL
2. Node Atemp
3. Hnode LocSrc, LocDst
4. LocSrc = Node_search(Graph_start, src)
5. LocDst = Node_search(Graph_start, dst)
6. ATemp = LocSrc ->ALINK
7. If LocSrc == NULL OR LocDst == NULL
8.
Retrun LOC
9. Else
10.
While Atemp!=NULL
11.
If ATemp->AINFO = = dst
12.
Set LOC = ATemp
13.
Return LOC
14.
Else
15.
ATemp = ATemp->NEXT
16. Return LOC
Team 1116_002, Data Structures
Insertion
Node Insertion –
Algorithm Insert_Node(Graph_start, Item): This algorithm inserts the node Item in
graph.
1. Hnode Temp, Temp1
2.
Temp = Allocate memory
3.
Temp->INFO = Item
4.
Temp->LINK = NULL
5.
Temp->ALINK = NULL
6.
IF Graph_start == NULL
7.
Graph_start = Temp;
8.
9.
10.
11.
12.
Else
Temp1 = Graph_start
While (Temp1->LINK) != NULL
Temp1= Temp1->LINK
Temp1->LINK = Temp
Team 1116_002, Data Structures
Insertion (Cont…)
Edge Insertion –
Algorithm Insert_Edge(Graph_start, src, dst): This algorithm inserts an edge in directed
graph. src is source vertex and dst is destination vertex.
1. Hnode LocSrc, LocDst, Temp
2. Node Temp2, Temp1
3. Temp1 = Allocate memory
4. Temp1->AINFO = dst
5. Temp1->NEXT = NULL
6. Temp = Graph_start
7. LocSrc = Node_search(Graph_start, src)
8. LocDst = Node_search(Graph_start, dst)
9. If LocSrc == NULL OR LocDst == NULL
10.
Exit
11. IF LocSrc -> ALINK == NULL
12.
LocSrc ->ALINK = Temp1
13. ELSE
14.
Temp2= LocSrc->ALINK
15.
While Temp2->NEXT != NULL
16.
Temp 2= Temp2->NEXT
17.
Temp2->NEXT = Temp1
Team 1116_002, Data Structures
Deletion
Algorithm Delete_Edge(Graph_start, src, dst): This algorithm deletes an edge from
directed graph. src is source and dst is destination vertex.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Hnode LocSrc, LocDst
Node Temp, Temp1
Set Flag = 0
LocSrc= Node_search(Graph_start, src)
LocDst= Node_search(Graph_start, dst)
If LocSrc == NULL or LocDst == NULL
Exit
Temp = LocSrc ->ALINK
If Temp-> AINFO == dst
LocSrc->ALINK = Temp->NEXT
Flag = 1
Else
Temp1 = Temp
Temp = Temp->NEXT
Team 1116_002, Data Structures
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
While Temp->NEXT != NULL
If Temp-> AINFO == dst
Temp1->NEXT = Temp->NEXT
Set Flag = 1
BREAK
Temp1 = Temp
Temp= Temp->NEXT
If Flag == 0 && Temp->AINFO == dst
Temp1->NEXT = NULL
Flag = 1
If Flag == 0
Display “No Edge”
Free Temp
Team 1116_002, Data Structures
Deletion (Cont…)
Algorithm Delete_Node(Graph_start, Item): This algorithm deletes a node and edges
connected with this node from directed graph. Item is a vertex to be deleted.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
Hnode LocNode, Temp1, Temp2, Temp3
Node Temp
LocNode= Node_search(Graph_start, Item)
If LocNode == NULL
Exit
Temp = LocNode ->ALINK
While Temp!=NULL
Delete_Edge(Graph_start, Item, Temp->AINFO)
Temp=Temp->NEXT
Temp3 = Graph_start
While Temp3!=NULL
Delete_Edge(Graph_start, Temp3->INFO, Item)
Temp3=Temp3->LINK
Temp1= Graph_start
If Graph_start == LocNode
Graph_start = Temp1->LINK
Else
Temp2=Temp1
Temp1=Temp1->LINK
Team 1116_002, Data Structures
20.
21.
22.
23.
24.
25.
26.
While Temp1!= NULL
If Temp1-> INFO == Item
Temp2->LINK = Temp1->LINK
BREAK
Temp2 = Temp1
Temp1= Temp1->LINK
Free Temp1
Team 1116_002, Data Structures
Traversal
Visiting and processing of each node once
Two ways - (for directed and undirected graph)
1. Breadth first search (BFS)
2. Depth first search (DFS)
Exhaustive search – can visit each node and edge more than once
Both of these are efficient to visit each node exactly once
Traversal algorithms start at some vertex.
Which vertex?
Graph don’t have any starting vertex or anything like that.
So we can start from any vertex
Team 1116_002, Data Structures
Since some graph algorithms do not require all vertices of a graph to be visited, so
we will define both BFS and DFS such that it is possible the algorithms:
Starting from any vertex, will not visit all vertices if the traversed graph is
disconnected
Starting from a particular vertex, may not visit all vertices if the traversed
graph is weakly connected
The idea in graph traversal is that we must mark each vertex when we first
visit it, and keep track of what have not yet completely explored.
Team 1116_002, Data Structures
Points –
1.
We must also maintain a structure containing all the vertices we have
discovered but not yet completely explored.
2.
Initially, only a single start vertex is considered to be discovered.
3.
To completely explore a vertex, we look at each edge going out of it.
4.
Every edge and vertex in the connected component is eventually visited.
This is called correctness of graph traversal.
5.
The order we explore the vertices depends upon what kind of data structure
is used i.e queue or stack.
6.
The BFS and DFS traversal of a graph G is not unique. A traversal depends
both on the starting vertex, and on the order of traversing the adjacent
vertices of each node.
Team 1116_002, Data Structures
Breadth First Search (BFS)
In this method, After visiting a vertex v, we must visit all its adjacent vertices
w1, w2, w3, ..., before going down next level to visit vertices adjacent to w1
etc.
The method can be implemented using a queue.
A boolean array is used to ensure that a vertex is enqueued only once.
Steps –
1. enqueue the starting vertex
2. while(queue is not empty)
3.
dequeue a vertex v from the queue
4.
visit v
5.
enqueue vertices adjacent to v that were never enqueued
Note:
Adjacent vertices can be enqueued in any order, but to obtain a unique
traversal, we will enqueue them in alphabetical order.
Team 1116_002, Data Structures
BFS Algorithm
Algorithm BFS ( S, AdcMat, n): This algorithm Traverse all connected vertices exactly once.
S is a node where from we have to start traversal. AdcMat is keeping connectivity
information of graph nodes. n is the number of nodes. visited array is keeping the
information (0, 1) of visited nodes (0 for not visited and 1 for visited) for corresponding
nodes. Note: Vertices are given as integer values starting from 1 – n.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
For i=0 to n-1
visited[i] = 0
// initially all set to false
Trav_Order[i]=0
enqueue ( Q, S )
visited[S-1]=1
Order = 0
While isempty( Q ) == 0
v = dequeue( Q )
Trav_Order[Order] = v
Order=Order+1
For i=0 to i=n-1
If (AdcMat [v-1] [i]!= 0 && visited[i]==0)
enqueue ( Q, i)
visited [i] = 1
Team 1116_002, Data Structures
BFS: Start with Node 5
5
2
0
1
3
7
6
4
5
Team 1116_002, Data Structures
BFS: Node one-away
5
2
0
1
3
7
6
4
5
Team 1116_002, Data Structures
BFS: Visit 1 and 2
5
2
0
1
3
7
6
4
5 1 2
Team 1116_002, Data Structures
BFS: Nodes two-away
5
2
0
1
3
7
6
4
5 1 2
Team 1116_002, Data Structures
BFS: Visit 0 and 4
5
2
0
1
3
7
6
4
5 1 2 0 4
Team 1116_002, Data Structures
BFS: Nodes three-away
5
2
0
1
3
7
6
4
5 1 2 0 4
Team 1116_002, Data Structures
BFS: Visit nodes 3 and 7
5
2
0
1
3
7
6
4
5 1 2 0 4 3 7
Team 1116_002, Data Structures
BFS: Node four-away
5
2
0
1
3
7
6
4
5 1 2 0 4 3 7
Team 1116_002, Data Structures
BFS: Visit 6
5
2
0
1
3
7
6
4
5 1 2 0 4 3 7 6
Team 1116_002, Data Structures
Example of BFS
BFS-tree:
Order
of
Travers
al
A B D E
I
C G F H
Team 1116_002, Data Structures
Complexity analysis of BFS
For a Graph G=(V, E) and n = |V| and m=|E|
When Adjacency List is used
Complexity is O(m + n)
When Adjacency Matrix is used
Scanning each row for checking the connectivity of a Vertex is in
order
O(n). So, Complexity is O(n2)
Team 1116_002, Data Structures
Depth First Search (DFS)
In this algorithm, we at a vertex v first visits v, then some neighbour w of v, then some
neighbour x of w that has not been visited before and so on.
When it gets stuck, the DFS backtracks until it finds the first vertex that still has a
neighbour that has not been visited before. It continues with this neighbour until it
has to backtrack again. Eventually, it will visit all vertices reachable from v.
Must keep track of vertices already visited to avoid cycles.
The method can be implemented using recursion or iteration.
Steps –
1. push the starting vertex onto the stack
2. while(stack is not empty)
3.
pop a vertex off the stack, call it v
4.
if v is not already visited, visit it
5.
push vertices adjacent to v, not visited, onto the stack
Note:
Adjacent vertices can be pushed in any order; but to obtain a unique traversal,
we will push them in reverse alphabetical order.
Team 1116_002, Data Structures
DFS Algorithm
Algorithm DFS ( S, AdcMat, n): This algorithm Traverse all connected vertices exactly once.
S is a node where from we have to start traversal. AdcMat is keeping connectivity
information of graph nodes. n is the number of nodes. visited array is keeping the
information (0, 1) of visited nodes (0 for not visited and 1 for visited) for corresponding
nodes.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
For i=0 to n-1
visited[i] = 0
// initially all set to false
Trav_Order[i]=0
push ( STK, S )
visited[S-1]=1
Order = 0
While isempty( STK ) == 0
v = pop( STK )
For j=0 to Order
If Trav_Order[j]==v
Break
If j>Order
Trav_Order[Order] = v
Order=Order+1
Team 1116_002, Data Structures
15.
16.
17.
18.
For i=0 to i=n-1
If (AdcMat [v-1] [i]!= 0 && !Found(Trav_Order, i))
push ( STK, i)
visited [i] = 1
Team 1116_002, Data Structures
DFS: Start with Node 5
5
2
0
1
3
7
5
6
4
Push 5
Team 1116_002, Data Structures
5
2
0
1
3
7
6
4
Pop/Visit/Mark 5
5
Team 1116_002, Data Structures
5
2
0
1
3
7
1
2
6
4
Push 2, Push 1
5
Team 1116_002, Data Structures
5
2
0
1
3
7
2
6
4
Pop/Visit/Mark 1
5 1
Team 1116_002, Data Structures
5
2
0
0
2
1
3
7
4
2
6
4
5 1
Team 1116_002, Data Structures
Push 4, Push 2, Push
0
5
2
0
2
1
3
7
4
2
6
4
Pop/Visit/Mark 0
5 1 0
Team 1116_002, Data Structures
5
3
2
0
7
2
1
3
7
4
2
6
4
Push 7, Push 3
5 1 0
Team 1116_002, Data Structures
5
2
0
7
2
1
3
7
4
2
6
4
Pop/Visit/Mark 3
5 1 0 3
Team 1116_002, Data Structures
5
2
2
0
7
2
1
3
4
7
2
6
4
Push 2
5 1 0 3
Team 1116_002, Data Structures
5
2
0
7
2
1
3
7
4
2
6
4
Pop/Mark/Visit 2
5 1 0 3 2
Team 1116_002, Data Structures
5
2
0
2
1
3
7
4
2
6
4
Pop/Mark/Visit 7
5 1 0 3 2 7
Team 1116_002, Data Structures
5
2
0
6
2
1
3
4
7
2
6
4
Push 6
5 1 0 3 2 7
Team 1116_002, Data Structures
5
2
0
2
1
3
7
4
2
6
4
Pop/Mark/Visit 6
5 1 0 3 2 7 6
Team 1116_002, Data Structures
5
2
0
1
3
7
4
2
6
4
Pop (don’t visit) 2
5 1 0 3 2 7 6
Team 1116_002, Data Structures
DFS: Start with Node 5
5
2
0
1
3
7
2
6
4
Pop/Mark/Visit 4
5 1 0 3 2 7 6 4
Team 1116_002, Data Structures
Preorder DFS: Start with Node 5
5
2
0
1
3
7
6
4
Pop (don’t visit) 2
5 1 0 3 2 7 6 4
Team 1116_002, Data Structures
Preorder DFS: Start with Node 5
5
2
0
1
3
7
6
4
Done
5 1 0 3 2 7 6 4
Team 1116_002, Data Structures
Example of DFS
DFS Tree:
Order of
Traversal
A B C
F
E G D H I
Team 1116_002, Data Structures
Complexity analysis of DFS
For a Graph G=(V, E) and n = |V| and m=|E|
When Adjacency List is used
Complexity is O(m + n)
When Adjacency Matrix is used
Scanning each row for checking the connectivity of a Vertex is in
order
O(n). So, Complexity is O(n2)
Team 1116_002, Data Structures
Spanning Tree
Let G is a weighted graph then G’ is said to be spanning tree of G which should follow
following condition –
1.
Should contain all vertices of G
2.
Should not have any cycle
3.
Should be connected
Definition: Spanning tree of a graph is a sub-graph with all vertices and no cycle.
A graph can have any number of spanning tree
Minimum cost spanning tree is a spanning tree whose sum of weights on edges is
minimum in all spanning tree
A connected,
undirected graph
Four of the spanning trees of the graph
Team 1116_002, Data Structures
Finding a spanning tree
To find a spanning tree of a graph,
pick an initial node and call it part of the spanning tree
do a search from the initial node:
each time you find a node that is not in the spanning tree, add to the
spanning tree both the new node and the edge you followed to get to it
An undirected graph
One possible
result of a BFS
starting from top
Team 1116_002, Data Structures
One possible
result of a DFS
starting from top
Finding minimum cost spanning trees
There are two basic algorithms for finding minimum-cost spanning trees, and both are
greedy algorithms –
1.
Kruskal’s algorithm
2.
Prim’s algorithm
Team 1116_002, Data Structures
Kruskal’s Algorithm
Start with no nodes or edges in the spanning tree, and repeatedly add the cheapest
edge that does not create a cycle
T = empty spanning tree
E = set of edges
N = number of nodes in graph
1.
While T has fewer than N - 1 edges
2.
remove an edge (v, w) of lowest cost from E
3.
if adding (v, w) to T would create a cycle
4.
5.
then discard (v, w)
else add (v, w) to T
Note: Finding an edge of lowest cost can be done just by sorting the edges
Team 1116_002, Data Structures
Prim’s Algorithm
Start with any one node in the spanning tree, and repeatedly add the cheapest edge,
and the node it leads to, for which the node is not already in the spanning tree.
Implementation is simpler than kruskal’s algorithm
T = a spanning tree containing a single node s
E = set of edges adjacent to s
1.
While T does not contain all the nodes
2.
remove an edge (v, w) of lowest cost from E
3.
if w is already in T
4.
5.
then discard edge (v, w)
else
6.
add edge (v, w) and node w to T
7.
add edges adjacent to w to E
Team 1116_002, Data Structures
Given Graph
4
B
4
A
Minimum Cost Spanning Tree
C
2
4
E
1
1
F
A
5
G
6
C
2
E
3
10
5
4
1
2
D
4
B
1
F
2
D
G
3
3
4
I
H
I
2
J
H
3
2
Team 1116_002, Data Structures
J
3
Shortest Path
Let we are given a directed graph where weight attached with each edge.
Our problem is to find out the path from any vertex V to another vertex W such that the
sum of weights is as small as possible. Such path is called shortest path.
From one specified source to one specified destination – single pair shortest path
From one specified source to all destination – single source shortest path
(Dijkstra’s)(Bellmanford if edges weights are negative)
From any source to any destination – all pair shortest path (Floyd-warshall)
Team 1116_002, Data Structures
Dijkstra’s Algorithm
Algorithm Dijkstra’s (source, Graph, dist) – This algorithm finds the shortest path from
source vertex to every other vertices. This is applicable to weighted graph. Dist is 1D array
containing distance information of vertices. Path array is containing the shortest path.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
For each vertex v in Graph
dist[v] = infinity
path[source] = NIL
dist[source] = 0
Q = the set of all nodes in Graph
While Q is not empty:
u = vertex in Q with smallest distance in dist[]
remove u from Q
If dist[u] == infinity
Break
For each neighbour v of u
if dist[u] + dist_between(u, v) < dist[v]
dist[v] = dist[u] + dist_between(u, v)
path[v]= u
Time Complexity = O(E + V2) = O(V2)
Team 1116_002, Data Structures
Example Dijkstra
1
B
C
7
2
3
S
8
4
5
2
A
D
5
Q
S
A
B
C
D
dist[v]
0
inf
inf
inf
inf
path[v]
nil
Team 1116_002, Data Structures
Example Dijkstra(cont…)
Here, adjacent or neighbour of S = {A,B}
B
1
C
7
For node A,
dist[S] + dist_between(S,A) < dist [A] therefore,
dist[A] = dist[S] + dist_between(S,A) = 0 + 2 = 2
3 2
S
8
4 5
2
For node B,
dist[S] + dist_between(S,B) < dist [B] therefore,
dist[B] = dist[S] + dist_between(S,B) = 0 + 7 = 7
A
Q
S
A
B
C
D
dist[v]
0
2
7
inf
inf
path[v]
nil
S
S
Team 1116_002, Data Structures
5
D
Example Dijkstra(cont…)
Here, adjacent or neighbour of A = {B,C,D}
B
1
C
7
For node B,
dist[A] + dist_between(A,B) < dist [B] therefore,
dist[B] = dist[A] + dist_between(A,B) = 2 + 2 = 4
3 2
S
8
4 5
2
For node C,
dist[A] + dist_between(A,C) < dist [C] therefore,
dist[C] = dist[A] + dist_between(A,C) = 2 + 8 = 10
A
For node D,
dist[A] + dist_between(A,D) < dist [D] therefore,
dist[D] = dist[A] + dist_between(A,D) = 2 + 5 = 7
Q
S
A
B
C
D
dist[v]
0
2
4
10
7
path[v]
nil
S
A
A
A
Team 1116_002, Data Structures
5
D
Example Dijkstra(cont…)
Here, adjacent or neighbour of B = {A,C}
B
1
C
7
For node A,
Condition False
dist[B] + dist_between(B,A) < dist [A]
3 2
S
8
4 5
2
For node C,
dist[B] + dist_between(B,C) < dist [C] therefore,
dist[C] = dist[B] + dist_between(B,C) = 4 + 1 = 5
A
Q
S
A
B
C
D
dist[v]
0
2
4
5
7
path[v]
nil
S
A
B
A
Team 1116_002, Data Structures
5
D
Example Dijkstra(cont…)
Here, adjacent or neighbour of C = {D}
B
1
C
7
For node D,
Condition False
dist[C] + dist_between(C,D) < dist [D]
3 2
S
8
4 5
2
A
Q
S
A
B
C
D
dist[v]
0
2
4
5
7
path[v]
nil
S
A
B
A
Team 1116_002, Data Structures
5
D
Example Dijkstra(cont…)
Here, adjacent or neighbour of D= {C}
B
1
C
7
For node C,
Condition False
dist[D] + dist_between(D,C) < dist [C]
3 2
S
8
4 5
2
A
Q
S
A
B
C
D
dist[v]
0
2
4
5
7
path[v]
nil
S
A
B
A
Team 1116_002, Data Structures
5
D
Topological sorting
Let S be a directed graph without cycle. Then a topological sort T of S is a linear ordering of
the nodes of S i.e. If u < v in S and there exist a path from u to v then u should come before v
in linear ordering.
Topological sorting can be defined as –
1.
Take all the nodes which have zero in degree.
2.
Delete those nodes and edges going from those nodes.
3.
Do the same process again until all the nodes are deleted.
Queue will be used to find out the topological order
A
F
B
E
G
A
C
A, C, G, D, B, E, F
C
G
D
Team 1116_002, Data Structures
D
B
E
F
Applications of graph
1.
Finding communities in networks
2.
Connectivity between cities
3.
GPS to find out shortest path
4.
Represent real time network
Team 1116_002, Data Structures
Summary
Introduction to graph
Types of graph
Traversal algorithms in graph
BFS
DFS
Spanning tree
Minimum cost spanning tree
Kruskal’s
Prim’s
Topological sorting
Applications
Team 1116_002, Data Structures
Download