Upload
ketakipattani
View
82
Download
0
Embed Size (px)
Citation preview
GraphPrepared By : Ketaki
PattaniEnroll. No. :
1302101070391
4
76
3 5
2
1 2
3
Some related terms:• Graph: A graph G is a pair (V,E), where V is a finite
set of points called vertices and E is a finite set of edges.
• Path: A path from a vertex v to a vertex u is a sequence <v0,v1,v2,…,vk> of vertices where v0 ,v1, v2… are the set of vertices followed to move from a source to destination.
• Pathlength:The length of a path is defined as the number of edges in the path.
Related terms: Degree of vertex• Degree of vertex: It represents
the total number of edges linked to a particular vertex.
• Indegree: It is the number of incomming edges to a vertex.
•Outdegree: It is the number of outgoing edges from a vertex.
•Source: A vertex having indegree zero is a source.
•Sink: A vertex having outdegree zero is a sink.
Some related terms:• Undirected Graph: An
undirected graph is the graph consisting of unordered pair of vertices . For eg :figure (a)
• Directed Graph : In a directed graph, the edge e is an ordered pair (u,v) which is incident from vertex u and is incident to vertex v. For eg : fig(b)
Some related terms: Weighted graph
• A weighted graph is a graph where each edge is assigned a particular value.
• This may be the distance or the cost.
• Here, the graph G(V,E) may be directed or undirected graph.
• Also the weight may be represented by ‘W’.
Graph Representation:Adjacency Matrix:• Here, a two dimensional
square matrix can be used to store a graph.
• Adjacency matrix for undirected graph is always symmetric .
Adjacency List• Adjacency List creates a
seperste linked list for each adjacent vertex.
• Adjacency graph for n nodes is represented by an Array of Pointers.
a
dc
b
a
bcd
b
a
d
d c
c
a b
a c
a
dc
b1 2
3 4
1 2 3 41 0 1 1 12 1 0 1 03 1 1 0 14 1 0 1 0
Graph Traversal:
• Most of the applications of graphs require the traversal of graphs.
• Traversal means visiting each node in a graph exactly once.
• It also stands for that there must be no cycles in the traversal.
• The two commonly used techniques are:(1) Depth First Search(DFS)(2) Breadth First Search(BFS)
Breadth First Search:• Strategy
choose a starting vertex, distance d = 0
vertices are visited in order of increasing distance from the starting vertex,
examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)
then, examine all edges leading from vertices at distance d+1 to distance d+2, and so on,
until no new vertex is discovered
Algorithm for BFS:1. for each vertex u in V[G] – {s}2 do color[u] white3 d[u] 4 [u] nil5 color[s] gray6 d[s] 07 [s] nil8 Q 9 enqueue(Q,s)10 while Q 11 do u dequeue(Q)12 for each v in Adj[u]13 do if color[v] = white14 then color[v] gray15 d[v] d[u] + 116 [v] u17 enqueue(Q,v)18 color[u] black
Example (BFS)
0
r s t u
v w x y
Q: s 0
(Courtesy of Prof. Jim Anderson)
Example (BFS)
1 0
1
r s t u
v w x y
Q: w r 1 1
Example (BFS)
1 0
1 2
2
r s t u
v w x y
Q: r t x 1 2 2
Example (BFS)
1 0
1 2
2
2
r s t u
v w x y
Q: t x v 2 2 2
Example (BFS)
1 0
1 2
2 3
2
r s t u
v w x y
Q: x v u 2 2 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: v u y 2 3 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: u y 3 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: y 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q:
Use of a queue in BFS Graph:• It is very common to use a queue to keep track
of:– nodes to be visited next, or– nodes that we have already visited.
• Typically, use of a queue leads to a breadth-first visit order.
• Breadth-first visit order is “cautious” in the sense that it examines every path of length i before going on to paths of length i+1.
Depth First Search:• Strategy
– choose a starting vertex, distance d = 0
– examine One edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)
– then, examine One edges leading from vertices at distance d+1 to distance d+2, and so on,
– until no new vertex is discovered, or dead end
– then, backtrack one distance back up, and try other edges, and so on.
Algorithm for DFS:
.
Example (DFS)
1/
u v w
x y z
Example (DFS)
1/ 2/
u v w
x y z
Example (DFS)
1/
3/
2/
u v w
x y z
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF C
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
1/8
4/5 3/6 10/11
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
1/8
4/5 3/6 10/11
2/7 9/12
u v w
x y z
BF C
B
Use of a stack in DFS Graph• It is very common to use a stack to keep track
of:– nodes to be visited next, or– nodes that we have already visited.
• Typically, use of a stack leads to a depth-first visit order.
• Depth-first visit order is “aggressive” in the sense that it examines complete paths.
Minimum Spanning Tree (MST)
• it is a tree (i.e., it is acyclic)• it covers all the vertices V
– contains |V| - 1 edges• the total cost associated with tree edges is the
minimum among all possible spanning trees
A minimum spanning tree is a subgraph of an undirected weighted graph G as shown, such that
Minimum Spanning Tree: Prim's Algorithm
• Prim's algorithm for finding an MST is a greedy algorithm.
• Start by selecting an arbitrary vertex, include it into the current MST.
• Grow the current MST by inserting into it the vertex closest to one of the vertices already in current MST.
Minimum Spanning Tree: Prim's Algorithm
.
Minimum Spanning Tree: Prim's Algorithm
.
Dijkstra's algorithm
• For a weighted graph G = (V,E,w), the single-source shortest paths problem is to find the shortest paths from a vertex v ∈ V to all other vertices in V.
• Dijkstra's algorithm is similar to Prim's algorithm. – maintains a set of nodes for which the shortest paths
are known.– grow this set based on the node closest to source using
one of the nodes in the current shortest path set.
Single-Source Shortest Paths: Dijkstra's Algorithm
.
Floyd's Algorithm
• For any pair of vertices vi, vj ∈ V, consider all paths from vi to vj whose intermediate vertices belong to the set {v1,v2,…,vk}.
• Let pi(,k
j) (of weight di
(,k
j)) be the minimum-weight path among
them. • If vertex vk is not in the shortest path from vi to vj, then pi
(,k
j) is
the same as pi(,k
j-1).
• If f vk is in pi(,k
j), then we can break pi
(,k
j) into two paths - one
from vi to vk and one from vk to vj . Each of these paths uses vertices from {v1,v2,…,vk-1}.
Floyd's Algorithm
From our observations, the following recurrence relation follows:
This equation must be computed for each pair of nodes and for k = 1, n. The serial complexity is O(n3).
Floyd's Algorithm
Floyd's all-pairs shortest paths algorithm. This program computes the all-pairs shortest paths
of the graph G = (V,E) with adjacency matrix A.