48
Graph Prepared By : Ketaki Pattani Enroll. No. : 130210107039 1 4 7 6 3 5 2 1 2 3

130210107039 2130702

Embed Size (px)

Citation preview

Page 1: 130210107039 2130702

GraphPrepared By : Ketaki

PattaniEnroll. No. :

1302101070391

4

76

3 5

2

1 2

3

Page 2: 130210107039 2130702

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.

Page 3: 130210107039 2130702

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.

Page 4: 130210107039 2130702

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)

Page 5: 130210107039 2130702

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’.

Page 6: 130210107039 2130702

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

Page 7: 130210107039 2130702

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)

Page 8: 130210107039 2130702

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

Page 9: 130210107039 2130702

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

Page 10: 130210107039 2130702

Example (BFS)

0

r s t u

v w x y

Q: s 0

(Courtesy of Prof. Jim Anderson)

Page 11: 130210107039 2130702

Example (BFS)

1 0

1

r s t u

v w x y

Q: w r 1 1

Page 12: 130210107039 2130702

Example (BFS)

1 0

1 2

2

r s t u

v w x y

Q: r t x 1 2 2

Page 13: 130210107039 2130702

Example (BFS)

1 0

1 2

2

2

r s t u

v w x y

Q: t x v 2 2 2

Page 14: 130210107039 2130702

Example (BFS)

1 0

1 2

2 3

2

r s t u

v w x y

Q: x v u 2 2 3

Page 15: 130210107039 2130702

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

Page 16: 130210107039 2130702

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q: u y 3 3

Page 17: 130210107039 2130702

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q: y 3

Page 18: 130210107039 2130702

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q:

Page 19: 130210107039 2130702

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.

Page 20: 130210107039 2130702

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.

Page 21: 130210107039 2130702

Algorithm for DFS:

.

Page 22: 130210107039 2130702

Example (DFS)

1/

u v w

x y z

Page 23: 130210107039 2130702

Example (DFS)

1/ 2/

u v w

x y z

Page 24: 130210107039 2130702

Example (DFS)

1/

3/

2/

u v w

x y z

Page 25: 130210107039 2130702

Example (DFS)

1/

4/ 3/

2/

u v w

x y z

Page 26: 130210107039 2130702

Example (DFS)

1/

4/ 3/

2/

u v w

x y z

B

Page 27: 130210107039 2130702

Example (DFS)

1/

4/5 3/

2/

u v w

x y z

B

Page 28: 130210107039 2130702

Example (DFS)

1/

4/5 3/6

2/

u v w

x y z

B

Page 29: 130210107039 2130702

Example (DFS)

1/

4/5 3/6

2/7

u v w

x y z

B

Page 30: 130210107039 2130702

Example (DFS)

1/

4/5 3/6

2/7

u v w

x y z

BF

Page 31: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6

2/7

u v w

x y z

BF

Page 32: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF

Page 33: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF C

Page 34: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

Page 35: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

B

Page 36: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6 10/11

2/7 9/

u v w

x y z

BF C

B

Page 37: 130210107039 2130702

Example (DFS)

1/8

4/5 3/6 10/11

2/7 9/12

u v w

x y z

BF C

B

Page 38: 130210107039 2130702

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.

Page 39: 130210107039 2130702

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

Page 40: 130210107039 2130702

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.

Page 41: 130210107039 2130702

Minimum Spanning Tree: Prim's Algorithm

.

Page 42: 130210107039 2130702

Minimum Spanning Tree: Prim's Algorithm

.

Page 43: 130210107039 2130702

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.

Page 44: 130210107039 2130702

Single-Source Shortest Paths: Dijkstra's Algorithm

.

Page 45: 130210107039 2130702

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}.

Page 46: 130210107039 2130702

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).

Page 47: 130210107039 2130702

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.

Page 48: 130210107039 2130702