Вы находитесь на странице: 1из 113

Graphs

Data Structures & Algo- Dr Ahmar Rashid 1


Graph Definitions
A graph G = (V, E) consists of
a set of vertices, V, and
a set of edges, E.
Each edge is a pair (v,w), where v, w V.
Edges ~ arcs.
If the pair is ordered, then the graph is directed.
Also referred to as digraphs.
Vertex w is adjacent to v if and only if (v,w) E.
In an undirected graph with edge (v,w), and hence
(w,v)
w is adjacent to v, and
v is adjacent to w.
Sometimes an edge has a third component, known as
either a weight or a cost.
Data Structures & Algo- Dr Ahmar Rashid 2
A graph Example
Data Structures & Algo- Dr Ahmar Rashid
3
v
6

v
7

v
5

v
2

v
3

v
4

v
1

A graph G = (V, E) consists of
a set of vertices, V = {v
1
, v
2
,v
3
,}
a set of edges, E.
E

A graph Example
Data Structures & Algo- Dr Ahmar Rashid
4
v
6
= (0,0)
v
7
= (9,1)
v
5

= (17,5)
v
2
= (19,11)
v
3

= (14,9)
v
4
= (8,6)
v
1
= (5,11)
12.1
10
9.1
8.9
9.1
14
9.2
6.3
5.4
5
5.8
The graph G is a Euclidean graph, if the weight if each
edge e
i
i.e., w(e
i
) = Euclidean distance between its
endpoints v
i
and v
j
.

Graph Path
A path in a graph is a sequence of vertices v
1
, v
2
, v
3
, . . . ,
v
n
such that (v
i
, v
i+i
) E for 1 i < n

The length of such a path (involving n vertices) is the
number of edges on the path, which is equal to n - 1

We allow a path from a vertex to itself; if this path contains
no edges, then the path length is 0.
If the graph contains an edge (v,v) from a vertex to itself,
then the path v, v is sometimes referred to as a loop.

The graphs we will consider will generally be loopless.

A simple path is a path such that all vertices are distinct,
except that the first and last could be the same.
Data Structures & Algo- Dr Ahmar Rashid 5
Cycles in the Graph
A cycle in a directed graph is a path of length at least 1 such that
w
1
= w
n

A path with no repeated vertices is called a simple path
A cycle with no repeated vertices or edges aside from the
necessary repetition of the start and end vertex is a simple cycle
For undirected graphs, we require that the edges be distinct
The logic of these requirements is that the path u, v, u in an
undirected graph should not be considered a cycle, because
(u, v) and (v, u) are the same edge
In a directed graph, these are different edges, so it makes
sense to call this a cycle

A directed graph is acyclic if it has no cycles
A directed acyclic graph is sometimes referred to by its
abbreviation, DAG
Data Structures & Algo- Dr Ahmar Rashid 6
A Connected Graph
An undirected graph is connected if there is a path
from every vertex to every other vertex
A directed graph with this property is called strongly
connected
If a directed graph is not strongly connected, but the
underlying graph (without direction to the arcs) is
connected, then the graph is said to be weakly
connected
A complete graph is a graph in which there is an edge
between every pair of vertices
Data Structures & Algo- Dr Ahmar Rashid 7
Applications of Graphs
Driving Map
Edge = Road
Vertex = Intersection
Edge weight = Time required to cover the road
Airline Traffic
Vertex = Cities serviced by the airline
Edge = Flight exists between two cities
Edge weight = Flight time or flight cost or both
Computer networks
Vertex = Server nodes
Edge = Data link
Edge weight = Connection speed
A Directed Graph Example
Data Structures & Algo- Dr Ahmar Rashid 9
1 2
6 7
3
4 5
Here,
V = {1, 2, 3, 4, 5, 6, 7}
E = {(1, 2), (1, 3), (1, 4), (2, 4), (2, 5), (3, 6), (4, 3),
(4, 6), (4, 7), (5, 4), (5, 7), (7, 6) }
A Directed Graph Example
Data Structures & Algo- Dr Ahmar Rashid 10
1 2
6 7
3 4 5
There is
a loop in the above graph
no loop in the above graph
All possible paths from 1 to 7 are
1 4 7
1 2 4 7
1 2 5 7
The above graph
has cycles ?
is acyclic ?
is a directed acyclic graph (DAG ) ?
Is this graph
connected ?
weakly connected ?
strong connected ?
complete ?
An undirected Graph Example
Data Structures & Algo- Dr Ahmar Rashid 11
1 2
6 7
3 4 5
The above graph
has cycles ?
is acyclic ?
is a directed acyclic graph (DAG ) ?
Is this graph
connected ?
weakly connected ?
strong connected ?
complete ?
There is
a loop in the above graph
no loop in the above graph
All possible paths from 1 to 7
(without cycles) are
1 4 7
1 4 5 7
1 4 6 7
1 4 2 5 7
1 4 3 6 7


1 2 5 7
1 2 5 4 7
1 2 5 4 6 7
1 2 5 4 3 6 7
1 2 4 7
1 2 4 5 7
1 2 4 6 7
1 2 4 3 6 7
1 3 6 7
1 3 4 7
1 3 4 5 7
1 3 4 6 7
1 3 4 2 5 7

Data Structures & Algo- Dr Ahmar Rashid 12
1 2
6 7
3 4 5
A Directed Graph Example
Adding a Link to a Directed Graph
Data Structures & Algo- Dr Ahmar Rashid 13
1 2
6 7
3
4 5
How to make the above graph strongly connected?
6 has no outgoing links
Create a directed link from 6 to 7
Does it solve the problem?
No: we can only come back to 6 from 7, and
From 6 , we cannot go anywhere except to 7

Adding a Link to a Directed Graph
Data Structures & Algo- Dr Ahmar Rashid 14
1 2
6 7
3
4 5
How to make the above graph strongly connected?
6 has no outgoing links
Create a directed link from 6 to 3
Does it solve the problem?
No: we can only come back to 6 from 3, and
From 6 , we cannot go anywhere except to 3

Adding a Link to a Directed Graph
Data Structures & Algo- Dr Ahmar Rashid 15
1 2
6 7
3
4 5
How to make the above graph strongly connected?
6 has no outgoing links
Create a directed link from 6 to 4
Does it solve the problem?
No: 1 has no incoming links

Adding a Link to a Directed Graph
Data Structures & Algo- Dr Ahmar Rashid 16
1 2
6 7
3
4 5
How to make the above graph strongly connected?
6 has no outgoing links
Create a directed link from 6 to 4
1 has no incoming links
Create a directed link from 4 to 1
Does it solve the problem?
Yes


Adding a Link to a Directed Graph
Data Structures & Algo- Dr Ahmar Rashid 17
1 2
6 7
3
4 5
How to make the above graph strongly connected?
6 has no outgoing links
1 has no incoming links
Is there a more efficient way to do this?
Yes
Create a directed link from 6 to 1

Representing graphs Adjacency
Matrix Representation
Data Structures & Algo- Dr Ahmar Rashid 18
Two dimensional matrix of size n x n where n is the number of vertices
in the graph

For each pair of vertices (u, v),
a[u][v] = 0; if there is no edge between u and v
a[u][v] = 1; if the edge exists between u and v
a[u][v] = w; If the edge has a weight w associated with it

Use either a very large or a very small weight as a sentinel to
indicate nonexistent edges.
For instance, if we were looking for the cheapest airplane route
a[u][v] = ; if there is no edge between u and v
If we were looking, for some strange reason, for the most
expensive airplane route
a[u][v] = - or 0; if there is no edge between u and v

Representing graphs Adjacency
Matrix Representation
Data Structures & Algo- Dr Ahmar Rashid 19
1 2
6 7
3 4 5
0 1 1 1 0 0 0
0 0 0 1 1 0 0
0 0 0 0 0 1 0
0 0 1 0 0 1 1
0 0 0 1 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 1 0
1 2 3 4 5 6 7
1
2
3
4
5
6
7
Adjacency Matrix Representation:
Space Requirements
Data Structures & Algo- Dr Ahmar Rashid 20
Adjacency Matrix Representation is extremely simple to
represent and program
However. the space complexity is very high i.e., (|V|
2
)
It can be prohibitive if the graph does not have very many edges.

An adjacency matrix is an appropriate representation if the
graph is dense: |E| = (|V|
2
) .

In most of the applications that we shall see, this is not true.
Example: Manhattan-like orientation
all the streets run either north-south or east-west
every intersection is attached to roughly four streets
if the graph is directed and all streets are two-way
|E| =4|V|.
3,000 intersections, 3,000 vertices with 12,000 edge entries
requires an array of size nine million.
most of these entries would contain zero.
Representing graphs: Adjacency
List Representation
Data Structures & Algo- Dr Ahmar Rashid 21
If the graph is not dense, in other words, if the graph is sparse,
a better solution is an adjacency list representation.
Adjacency lists are the standard way to represent graphs.
for each vertex, we keep a list of all adjacent vertices.
If the edges have weights, then this additional information is also
stored in the cells.

The space requirement is then O(|E| + |V|).

Undirected graphs can be similarly represented;
each edge (u, v) appears in two lists, so the space usage essentially
doubles.

A common requirement in graph algorithms is to find all vertices
adjacent to some given vertex v,
this can be done, in time proportional to the number of such vertices
found, by a simple scan down the appropriate adjacency list.

3
Representing graphs: Adjacency
List Representation
Data Structures & Algo- Dr Ahmar Rashid 22
1 2
6 7
3 4 5
1
2
3
4
5
6
7
2
4
NULL
6 3
7
NULL
5 4 NULL
7 4 NULL
6 NULL
6 NULL
NULL
Implementing graphs: Adjacency
List Representation
Data Structures & Algo- Dr Ahmar Rashid 23
struct node;
struct edge;
struct node
{
int name;
node *next;
edge *EHead;
edge *ETail;
}*head,*tail;

struct edge
{
int nextNode;
int cost;
edge *nextEdge;
};

name next
name next
name next
name next
name next
name next
name next
nextNode cost nextEdge
EHead
EHead
EHead
EHead
EHead
EHead
EHead
NULL
head
tail
NULL
nextNode cost nextEdge NULL
nextNode cost nextEdge NULL
nextNode cost nextEdge NULL
nextNode cost nextEdge NULL
nextNode cost nextEdge NULL
nextNode cost nextEdge NULL
nextEdge
3 10
Representing graphs: Adjacency
List Representation
Data Structures & Algo- Dr Ahmar Rashid 24
1 2
6 7
3 4 5
1
2
3
4
5
6
7
2 5
5
10 10
4
6
4 10 NULL
6 4 3 6 7 8 NULL
5 6 4 4 NULL
6 15
NULL
7 2 4 3 NULL
6 8 NULL
NULL
15
4
8
6
3
2
8
Graphs: Adjacency List
Append a new node
Data Structures & Algo- Dr Ahmar Rashid 25
void Append ( int name )
{
node *ptr=new node;
ptr->name=name;
ptr->EHead=new edge;
ptr->EHead->nextEdge=NULL;
ptr->ETail= ptr-> EHead;
ptr->next=NULL;
tail->next=ptr;
tail=ptr;
}

4 10
Data Structures & Algo- Dr Ahmar Rashid 26
void Append ( int name ) {
node *ptr=new node;
ptr->name=name;
ptr->next=NULL;
ptr->EHead=new edge;
ptr->EHead->nextEdge=NULL;
ptr->ETail= ptr-> EHead;
tail->next=ptr;
tail=ptr;
}

Main(){
Append(5);
}


1 next
2 next
3 next
4 next 3 6
4 4
NULL
NULL
NULL
NULL
1 2
3 4
5
10 10
4
6
3 10 2 5
NULL
head
tail
Graphs: Adjacency List
Append a new node
nextEdge
4 10
Data Structures & Algo- Dr Ahmar Rashid 27
1 next
2 next
3 next
4 next 3 6
4 4
NULL
NULL
NULL
3 10 2 5
NULL
void Append ( int name ) {
node *ptr=new node;
ptr->name=name;
ptr->next=NULL;
ptr->EHead=new edge;
ptr->ETail= ptr-> EHead;
ptr->EHead->nextEdge=NULL;
tail->next=ptr;
tail=ptr;
}

Main(){
Append(5);
}


NULL
5 next
NULL
tail
ptr
Graphs: Adjacency List
Append a new node
1 2
3 4
5
10 10
4
6
5
head
nextEdge
ptr->EHead
ptr->ETail
Data Structures & Algo- Dr Ahmar Rashid 28
edge *AddEdge (int nextNode, int cost, edge *ETail )
{
edge *E=new edge;
E->cost=cost;
E->nextNode=nextNode;
E->nextEdge=NULL;
ETail->nextEdge=E;
ETail=E;
return ETail;
}
Graphs: Adjacency List
Add a new edge
4 10
Data Structures & Algo- Dr Ahmar Rashid 29
1 next
2 next
3 next
4 next 3 6
4 4
NULL
NULL
NULL
3 10 2 5
NULL
main(){


If(ptr->name ==5)
ptr->ETail =AddEdge(4 ,7, ptr->ETail);
}

edge *AddEdge ( int nextNode, int cost, edge *ETail )
{
edge *E=new edge;
E->nextNode=nextNode;
E->cost=cost;
E->nextEdge=NULL;
ETail->nextEdge=E;
ETail=E;
return ETail;
}


NULL
5 next
NULL
head
tail
ptr
Graphs: Adjacency List
Add a new edge
1 2
3 4
5
10 10
4
6
5
nextEdge
4 7
7
Graph Traversal Algorithms
Data Structures & Algo- Dr Ahmar Rashid 30
Graph traversal refers to the problem of visiting
all the nodes in a graph in a particular manner

Tree traversal is a special case of graph
traversal.

In contrast to tree traversal, in general

graph traversal, each node may have to be visited
more than once,

and a root-like node that connects to all other
nodes might not exist
Breadth First Search
Data Structures & Algo- Dr Ahmar Rashid 31
In graph theory, breadth-first search (BFS) is a graph
search algorithm that begins at the root node and
explores all the neighboring nodes.
Then for each of those nearest nodes, it explores
their unexplored neighbor nodes, and so on, until it
fulfills its objective (i.e., visit all the nodes or reach
the destination node, if any).
From the standpoint of the algorithm, all child nodes
obtained by expanding a node are added to a FIFO
(i.e., First In, First Out) queue.
In typical implementations,
nodes that have not yet been examined for their neighbors
are placed in some container (such as a queue or linked list)
and have special color (such as white) or status (such as
unprocessed, not visited etc)
then once examined are placed in another container and
marked as processed, visited and etc.
BFS Algorithm
Data Structures & Algo- Dr Ahmar Rashid 32
1. Enqueue the root node.
2. Dequeue a node and examine it.
a) If the element sought is found in this node, quit
the search and return a result.
b) Otherwise enqueue any successors (the direct
child nodes) that have not yet been discovered.
3. If the queue is empty, every node on the graph
has been examined quit the search and return
"not found".
4. If the queue is not empty, repeat from Step 2.

BFS Pseudocode
Data Structures & Algo- Dr Ahmar Rashid 33
procedure BFS(G, s)
for each vertex u in G (except s)
u.status = notVisited/white
u.d =
u.pi = NULL
s.status = visited / grey
s.d = 0
s.pi = NULL
create a an empty queue Q
enqueue (Q , s)
while Q is not empty do
u = dequeue(Q)
for each v adjacent to u do
if v.status = notVisited/white
v.status = visited / grey
v.d = u.d + 1;
v.pi = u;
enqueue (Q,v)
u.status = processed/ black
BFS Example

r

s

u t

v

w

y x
for each vertex u in G (except s)
u.status = notVisited/white
u.d =
u.pi = NULL
BFS Example

r
0
s

u t

v

w

y x
Q
s
s.status = visited / grey
s.d = 0
s.pi = NULL
create a an empty queue Q
enqueue (Q , s)
while Q is not empty do
u = dequeue(Q)
for each v adjacent to u do
if v.status = notVisited/white
v.status = visited / grey
v.d = u.d + 1;
v.pi = u;
enqueue (Q,v)
u.status = processed/ black
1
BFS Example
1
r
0
s

u t

v
1
w

y x
Q
w r
1 1
s = dequeue(Q)
w.status = visited / grey
w.d = 0 + 1;
w.pi = s;
enqueue (Q,w)

r.status = visited / grey
r.d = 0 + 1;
r.pi = s;
enqueue (Q,r)
s.status = processed/ black


0
BFS Example
1
r
0
s
2
u t

v
1
w
2
y x
Q
r t x
1 2 2
w = dequeue(Q)
t.status = visited / grey
t.d = 1 + 1 = 2;
t.pi = w;
enqueue (Q, t)

x.status = visited / grey
x.d = 1 + 1 = 2;
x.pi = w;
enqueue (Q,x)
w.status = processed/ black
BFS Example
1
r
0
s
2
u t
2
v
1
w
2
y x
Q
t x v
2 2 2
r = dequeue(Q)
v.status = visited / grey
v.d = 1 + 1 = 2;
v.pi = r;
enqueue (Q, v)

r.status = processed/ black
BFS Example
1
r
0
s
2 3
u t
2
v
1
w
2
y x
Q
x v u
2 2 3
t = dequeue(Q)
u.status = visited / grey
u.d = 2 + 1 = 3;
u.pi = t;
enqueue (Q, u)

t.status = processed/ black
BFS Example
1
r
0
s
2 3
u t
2
v
1
w
2 3
y x
Q
v u y
2 3 3
x = dequeue(Q)
y.status = visited / grey
y.d = 2 + 1 = 3;
y.pi = x;
enqueue (Q, y)

x.status = processed/ black
BFS Example
1
r
0
s
2 3
u t
2
v
1
w
2 3
y x
Q
u y
2 3
v = dequeue(Q)
v = processed/ black
BFS Example
1
r
0
s
2 3
u t
2
v
1
w
2 3
y x
Q
y
3
u = dequeue(Q)
u = processed/ black
BFS Example
1
r
0
s
2 3
u t
2
v
1
w
2 3
y x
Q
y = dequeue(Q)
y = processed/ black
BFS Example 2
Q





5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
BFS Example 2
Q
1
0




5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
0
BFS Example 2
Q
2 3 4
0 5

10
10

5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
5 10 10
BFS Example 2
Q
3 4 5
0 5

10
10 11
5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
10 10 11
BFS Example 2
Q
4 5 6
0 5
25

10
10 11
5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
10 11 25
BFS Example 2
Q
5 6 7
0 5
25 18
10
10 11
5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
11 25 18
BFS Example 2
Q
6 7
0 5
25 18
10
10 11
5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
25 18
BFS Example 2
Q
7
0 5
25 18
10
10 11
5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
18
BFS Example 2
Q
0 5
25 18
10
10 11
5
10 10
4
6
15
4
8
6
3
2
8
1 2
3 4 5
6 7
Depth First Search (DFS)
Formally, DFS is an uninformed search that
progresses by
expanding the first child node of the search tree
(or graph) that appears, and
thus going deeper and deeper
until a goal node is found, or until it hits a node
that has no children.
Then the search backtracks, returning to the
most recent node it hasn't finished exploring.
In a non-recursive implementation, all freshly
expanded nodes are added to a stack for
exploration.

DFS Pseudocode
Data Structures & Algo- Dr Ahmar Rashid 54
procedure DFS(G, s)
for each vertex u in G
u.status = notVisited/white
u.pi = NULL
time = 0;
for each vertex u in G
if u.status = notVisited/white
DFS_visit(G, u)

DFS Pseudocode
Data Structures & Algo- Dr Ahmar Rashid 55
procedure DFS_visit(G, u)
u.status = visited / grey
time = time + 1;
u.d = time;
for each v adjacent to u do
if v.status = notVisited/white
v.pi = u;
DFS_visit(G, v)
u.status = processed/ black

DFS Example
0
r

s

u t

v

w

y x
DFS Example
0
r
1
s

u t

v

w

y x
DFS Example
0
r
1
s

u t

v
2
w

y x
DFS Example
0
r
1
s
3
u t

v
2
w

y x
DFS Example
3 4
u t

y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s

v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s
1
v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s
1
v
2
w
DFS Example
3 4
u t
6 5
y x
0
r
1
s
1
v
2
w
DFS: Non-recursive Implementation
1. Initialize all nodes to the notVisited state (STATUS=1)
2. PUSH starting node A on stack and change its status to
visited(STATUS=2)
3. Repeat 4-5 until stack is empty
4. POP N. Process it and change its status to
processed(STATUS=3)
5. PUSH on stack all the neighbors of N that are
still in notVisited state and change their status
to visited
6. Exit
DFS Example
0
r

s

u t

v

w

y x
r
DFS Example
0
r
1
s

u t
v

w

y x
v
s
1
DFS Example
0
r
1
s

u t
v
2
w

y x
v
w
1
DFS Example
0
r
1
s
3
u t
v
2
w
3
y x
v
x
1
t
DFS Example
0
r
1
s
3 4
u t
v
2
w
3
y x
v
x
1
u
DFS Example
0
r
1
s
3 4
u t
v
2
w
3 5
y x
v
x
1
y
DFS Example
0
r
1
s
3 4
u t
v
2
w
3 5
y x
v
x
1
DFS Example
0
r
1
s
3 4
u t
v
2
w
3 5
y x
v
1
DFS Example
0
r
1
s
3 4
u t
v
2
w
3 5
y x
1
Applications of BFS and DFS
Path finding
Find path from source vertex s to destination
vertex d
Shortest path calculation
Find the shortest 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
Use DFS?
USE BFS?
Applications of BFS and DFS
Shortest path calculation
vertices represent computers
edges link between computers
cost communication costs
financial costs (phone bill per 1,000 bytes of data)
delay costs (number of seconds required to
transmit 1,000 bytes), or
a combination of these and other factors
Use shortest-path algorithm to find the
cheapest way to send electronic news from
one computer to a set of other computers
Model airplane or other mass transit routes
by graphs
Use a shortest-path algorithm to compute the
best route between two points
Applications of BFS and DFS
Path finding in an un weighted graph
Find any path between two nodes?
DFS
Implicit path calculation
BFS
Need to store path explicitly


Applications DFS
Path finding in an un weighted graph
Find the shortest path between two
nodes?
BFS
Need to store path explicitly


Route Calculation
Dijkstras shortest path algorithm
Dijkstras algorithm
N : set of nodes for which shortest path already found
Initialization: (Start with source node s)
N = {s}, D
s
= 0, s is distance zero from itself
D
j
=C
sj
for all j s, distances of directly-connected
neighbors
Step A: (Find next closest node i )
Find i N such that
D
i
= min Dj for j N
Add i to N
If N contains all the nodes, stop
Step B: (update minimum costs)
For each node j N
D
j
= min (D
j
, D
i
+C
ij
)
Go to Step A
Minimum distance from s
to j through node i in N
Execution of Dijkstras algorithm
Iteration N D
2
D
3
D
4
D
5
D
6

Initial {1} 3 2 5
1 {1,3} 3 2 4 3
2 {1,2,3} 3 2 4 7 3
3 {1,2,3,6} 3 2 4 5 3
4 {1,2,3,4,6} 3 2 4 5 3
5 {1,2,3,4,5,6} 3 2 4 5 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3









Shortest Paths in Dijkstras Algorithm
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
1
2
4
5
6
1
1
2
3
2
3
5
2
4
3 3
Dijkstras algorithm: example
Step
0
1
2
3
4
5
start N
A
AD
ADE
ADEB
ADEBC
ADEBCF
D(B),p(B)
2,A
2,A
2,A
D(C),p(C)
5,A
4,D
3,E
3,E
D(D),p(D)
1,A
D(E),p(E)
infinity
2,D
D(F),p(F)
infinity
infinity
4,E
4,E
4,E
A
E D
C B
F
2
2
1
3
1
1
2
5
3
5
Minimum
Spanning Tree
Data Structures & Algo- Dr Ahmar Rashid 91
Minimum Spanning Tree (MST)
A minimum spanning tree of an undirected graph G is a
tree formed from graph edges that connects all the
vertices of G at lowest total cost.
A minimum spanning tree exists if and only if G is
connected
1 2
6 7
3
4 5
2
1
4
5
4
2
1
3
7
6
8
10
1 2
6 7
3
4 5
2
1
4
2
1
6
A graph G MST of G
Prims Algorithm
One way to compute a minimum spanning tree is to grow
the tree in successive stages.
In each stage,
1. Pick a node as the root,
2. add an edge, and
3. the associated vertex, to the tree.
At any point in the algorithm, we have
a set of vertices that have already been included in the tree;
the rest of the vertices are not in the tree yet
At each stage, the algorithm finds
a new vertex to add to the tree by choosing the edge (u, v)
such that the cost of (u, v) is the smallest among all edges where
u is in the tree and v is not.
Same as Dijkstras algo?

MST-Prim(G, w, r)
Q = V[G];
for each u Q
D[u] = ;
D[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < D[v])
p[v] = u;
D[v] = w(u,v);
Prims Algorithm
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

v
1
v
2
v
3
v
4
v
5
v
6
v
7
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
2
4
1
v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
v
1
v
2
v
3
v
4
v
5
v
6
v
7
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
4
5
4
2
1
3
7
6
8
10
1
1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
v
1
v
2
v
3
v
4
v
5
v
6
v
7
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
4
5
4
2
1
3
7
6
8
10
1
1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
v
1
v
2
v
3
v
4
v
5
v
6
v
7
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
5
4
2
1
3
7
6
8
10
1
4
1,4,2,3
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
5

, v
3
4

, v
4

v
1
v
2
v
3
v
4
v
5
v
6
v
7
1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
5
4
2
1
3
7
6
8
10
1
4
1,4,2,3,7
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

v
1
v
2
v
3
v
4
v
5
v
6
v
7
1,4,2,3
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
5

, v
3
4

, v
4

1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
5
4
2
1
3
7
6
8
10
1
4
1,4,2,3,7,6
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

v
1
v
2
v
3
v
4
v
5
v
6
v
7
v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1,4,2,3,7
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

1,4,2,3
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
5

, v
3
4

, v
4

1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
4
5
4
2
1
3
7
6
8
10
1
1 to 7
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

v
1
v
2
v
3
v
4
v
5
v
6
v
7
1,4,2,3,7,6
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1,4,2,3,7
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

1,4,2,3
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
5

, v
3
4

, v
4

1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
Prims Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
4
1
6
1
2
1,4,2,3
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
5

, v
3
4

, v
4

1,4
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2
0

, 0 2

, v
1
2

, v
4
1

, v
1
7

, v
4
8

, v
4
4

, v
4

1,4,2,3,7
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

1,4,2,3,7,6
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

1 to 7
0

, 0 2

, v
1
2

, v
4
1

, v
1
6

, v
7
1

, v
7
4

, v
4

v
D
1
, p
1
D
2
, p
2
D
3
, p
3
D
4
, p
4
D
5
, p
5
D
6
, p
6
D
7
, p
7

1
0

, 0 2

, v
1
4

, v
1
1

, v
1


, 0

, 0

, 0
v
1
v
2
v
3
v
4
v
5
v
6
v
7
Kruskals Algorithm
create a forest F (a set of trees), where each vertex in
the graph is a separate tree
create a set E containing all the edges in the graph
while E is nonempty and F is not yet spanning
remove an edge with minimum weight from E
if that edge connects two different trees, then add it to
the forest, combining two trees into a single tree
otherwise discard that edge.
At the termination of the algorithm, the forest has only
one component and forms a minimum spanning tree of
the graph

Source: http://en.wikipedia.org/wiki/Kruskal's_algorithm
Kruskals Algorithm
MST-Kruskal (G,w)
F = { }
For each vertex v V[G]
MakeSet(v)
sort the edges(E) in increasing order by
weight w
for each edge (u,v) E
if FindSet(u) FindSet(v)
F = F U {(u,v)}
UNION(u,v)

Reference:http://www.personal.kent.edu/~rmuhamma/Algorithms
/MyAlgorithms/GraphAlgor/kruskalAlgor.htm
Kruskals Algorithm
MakeSet(v): Create a new set whose only
member is pointed to by v. Note that for this
operation v must already be in a set.
FindSet(v): Returns a pointer to the set
containing v.
UNION(u, v): Unites the dynamic sets that
contain u and v into a new set that is union of
these two sets.

v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
1
Min Edge = ( v
1
, v
4
)

FindSet( v
1
) FindSet( v
4
)

F = F U {( v
1
, v
4
)}

UNION( v
1
, v
4
)
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
1
1
Min Edge = ( v
6
, v
7
)

FindSet( v
6
) FindSet( v
7
)

F = F U {( v
6
, v
7
)}

UNION( v
6
, v
7
)
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
1
2
1
Min Edge = ( v
1
, v
2
)

FindSet( v
1
) FindSet( v
2
)

F = F U {( v
1
, v
2
)}

UNION( v
1
, v
2
)
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
1
2
2
1
Min Edge = ( v
3
, v
4
)

FindSet( v
3
) FindSet( v
4
)

F = F U {( v
3
, v
4
)}

UNION( v
3
, v
4
)
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
1
2
2
1
4
Min Edge = ( v
4
, v
7
)

FindSet( v
4
) FindSet( v
7
)

F = F U {( v
4
, v
7
)}

UNION( v
4
, v
7
)
v
1
v
2
v
6
v7
v
3
v
4
v
5
2
1
4
5
4
2
1
3
7
6
8
10
v
1
v
2
v
6
v
7
v
3
v
4
v
5
Kruskals Algorithm
1
2
2
1
4
6
Min Edge = ( v
7
, v
5
)

FindSet( v
7
) FindSet( v
5
)

F = F U {( v
7
, v
5
)}

UNION( v
7
, v
5
)

Вам также может понравиться