93
GRAPH THEORY Prof S Sameen Fatima Dept of Computer Science and Engineering Osmania University College of Engineering Hyderabad 500007 [email protected] Graph Theory S Sameen Fatima 1

graph theory

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: graph theory

S Sameen Fatima 1

GRAPH THEORY

Prof S Sameen FatimaDept of Computer Science and EngineeringOsmania University College of Engineering

Hyderabad [email protected]

Graph Theory

Page 2: graph theory

S Sameen Fatima 2

OVERVIEW

• BASICS• REPRESENTATION OF GRAPHS• MINIMUM SPANNING TREE• SEARCH ALGORITHMS• EXAMPLES

Graph Theory

Page 3: graph theory

BASICS

1. What Is a Graph?

2. Kinds of Graphs

3. Vertex Degree

4. Paths and Cycles

Graph Theory S Sameen Fatima 3

Page 4: graph theory

The KÖnigsberg Bridge Problem • Königsber is a city on the Pregel river in

Prussia

• The city occupied two islands plus areas on both banks

• Problem: Whether they could leave home, cross every bridge exactly once, and return home.

Graph Theory S Sameen Fatima 4

X

Y

Z

W

Page 5: graph theory

A Model

• A vertex : an island

• An edge : a path(bridge) between two islands

Graph Theory S Sameen Fatima 5

e1e2

e3

e4

e6

e5

e7

Z

Y

X

W

X

Y

Z

W

Page 6: graph theory

General Model

• A vertex : an object

• An edge : a relation between two objects

Graph Theory S Sameen Fatima 6

common

member

Committee 1 Committee 2

Page 7: graph theory

What Is a Graph?

• A graph G is an ordered pair (V, E) consisting of:– A vertex set V = {W, X, Y, Z}– An edge set E = {e1, e2, e3, e4, e5, e6, e7}

Graph Theory S Sameen Fatima 7

e1e2

e3

e4

e6

e5

e7

Z

Y

X

W

Page 8: graph theory

S Sameen Fatima 8

What Is a Graph?

• A graph, G is an ordered triple (V, E, f)

consisting of– V is a set of nodes, points, or vertices. – E is a set, whose elements are known as

edges or lines. – f is a function that maps each element

of E to an unordered pair of vertices in V.

Graph Theory

Page 9: graph theory

Loop, Multiple edges

• Loop : An edge whose endpoints are equal

• Multiple edges : Edges have the same pair of endpoints

Graph Theory S Sameen Fatima 9

loop

Multiple edges

Page 10: graph theory

Graph Theory S Sameen Fatima 10

Simple Graph

Simple graph : A graph has no loops or multiple

edges

loopMultiple edges

It is not simple. It is a simple graph.

Page 11: graph theory

Adjacent, neighbors• Two vertices are adjacent and are

neighbors if they are the endpoints of an edge

• Example:– A and B are adjacent– A and D are not adjacent

Graph Theory S Sameen Fatima 11

A B

C D

Page 12: graph theory

Finite Graph, Null Graph

• Finite graph : an graph whose vertex set and edge set are finite

• Null graph : the graph whose vertex set and edges are empty

Graph Theory S Sameen Fatima 12

Page 13: graph theory

Connected and Disconnected

• Connected : There exists at least one path between two vertices

• Disconnected : Otherwise

• Example: – H1 and H2 are connected

– H3 is disconnected

Graph Theory S Sameen Fatima 13

c

d

a b

de

a b

c

deH1

H3H2

Page 14: graph theory

S Sameen Fatima 14

Complete Graph

• Complete Graph: A simple graph in which every pair of vertices are adjacent

• If no of vertices = n, then there are n(n-1) edges

Graph Theory

Page 15: graph theory

S Sameen Fatima 15

Sparse/Dense Graph

• A graph is sparse if | E | | V |• A graph is dense if | E | | V |2.

Graph Theory

Page 16: graph theory

S Sameen Fatima 16

Directed Graph (digraph)

In a digraph edges have directions

Graph Theory

Page 17: graph theory

S Sameen Fatima 17

Weighted Graph

Weighted graph is a graph for which each edge has an associated weight, usually given by a weight function w: E R.

Graph Theory

1 2 3

4 5 6

.5

1.2

.2

.5

1.5.3

1

4 5 6

2 32

135

Page 18: graph theory

S Sameen Fatima 18

Planar Graph

• Can be drawn on a plane such that no two edges intersect

Graph Theory

Page 19: graph theory

Complement

Complement of G: The complement G’ of a simple graph G :– A simple graph– V(G’) = V(G) – E(G’) = { uv | uv E(G) }

Graph Theory S Sameen Fatima 19

G

u

v

wx

y

G’

u

v

wx

y

Page 20: graph theory

Subgraphs

• A subgraph of a graph G is a graph H such that:– V(H) V(G) and E(H) E(G) and– The assignment of endpoints to edges

in H is the same as in G.

Graph Theory S Sameen Fatima 21

Page 21: graph theory

Subgraphs

• Example: H1, H2, and H3 are subgraphs of G

Graph Theory S Sameen Fatima 22

c

d

a b

de

a b

c

deH1

G

H3H2

a b

c

de

Page 22: graph theory

Bipartite Graphs• A graph G is bipartite if V(G) is the union

of two disjoint independent sets called partite sets of G

• Also: The vertices can be partitioned into two sets such that each set is independent

• Matching Problem

• Job Assignment Problem

Graph Theory S Sameen Fatima 23

Workers

Jobs

Boys

Girls

Page 23: graph theory

Chromatic Number

• The chromatic number of a graph G, written x(G), is the minimum number of colors needed to label the vertices so that adjacent vertices receive different colors

Graph Theory S Sameen Fatima 24

Red

Green

Blue

Bluex(G) = 3

Page 24: graph theory

Maps and coloring

• A map is a partition of the plane into connected regions

• Can we color the regions of every map using at most four colors so that neighboring regions have different colors?

• Map Coloring graph coloring– A region A vertex– Adjacency An edge

Graph Theory S Sameen Fatima 25

Page 25: graph theory

Scheduling and Graph Coloring

• Two committees can not hold meetings at the same time if two committees have common member

Graph Theory S Sameen Fatima 26

common

member

Committee 1 Committee 2

Page 26: graph theory

Scheduling and Graph Coloring• Model:– One committee being represented by a

vertex– An edge between two vertices if two

corresponding committees have common member

– Two adjacent vertices can not receive the same color

Graph Theory S Sameen Fatima 27

common

member

Committee 1 Committee 2

Page 27: graph theory

Scheduling and Graph Coloring

• Scheduling problem is equivalent to graph coloring problem

Graph Theory S Sameen Fatima 28

Common

MemberCommittee 1

Committee 2

Committee 3

Common Member

Different Color

No Common Member

Same Color OK

Same time slot OK

Page 28: graph theory

S Sameen Fatima 29

Degree

Degree: Number of edges incident on a node

Graph Theory

A

D E F

B C

The degree of B is 2.

Page 29: graph theory

S Sameen Fatima 30

Degree (Directed Graphs)• In degree: Number of edges entering a node• Out degree: Number of edges leaving a node• Degree = Indegree + Outdegree

Graph Theory

1 2

4 5

The in degree of 2 is 2 andthe out degree of 2 is 3.

Page 30: graph theory

S Sameen Fatima 31

Degree: Simple Facts

• If G is a digraph with m edges, then

indeg(v) = outdeg(v) = m = |E |

• If G is a graph with m edges, then

deg(v) = 2m = 2 |E |

– Number of Odd degree Nodes is even

Graph Theory

Page 31: graph theory

S Sameen Fatima 32

Path• A path is a sequence of vertices such that there

is an edge from each vertex to its successor. • A path is simple if each vertex is distinct.• A circuit is a path in which the terminal vertex

coincides with the initial vertex

Graph Theory

1 2 3

4 5 6

Simple path: [ 1, 2, 4, 5 ]Path: [ 1, 2, 4, 5, 4]Circuit: [ 1, 2, 4, 5, 4, 1]

Page 32: graph theory

S Sameen Fatima 33

Cycle

• A path from a vertex to itself is called a cycle. • A graph is called cyclic if it contains a cycle; – otherwise it is called acyclic

Graph Theory

1 2 3

4 5 6

Cycle

Page 33: graph theory

Other Paths• Geodesic path: shortest path

– Geodesic paths are not necessarily unique: It is quite possible to have more than one path of equal length between a given pair of vertices

– Diameter of a graph: the length of the longest geodesic path between any pair of vertices in the network for which a path actually exists

• Eulerian path: a path that traverses each edge in a network exactly once

The Königsberg bridge problem

• Hamilton path: a path that visits each vertex in a network exactly once

34

Page 34: graph theory

S Sameen Fatima 35

Euclerian Path

• An undirected graph possesses an Euclerian Path if and only if it is connected and has either zero or two vertices of odd degree

OR• An undirected graph possesses an Euclerian Path if

and only if it is connected and its vertices are all of even degree

There is no Euclerian Path for the Konigsberg Bridge Problem

Graph Theory

Page 35: graph theory

S Sameen Fatima 36

GRAPH REPRESENTATION

• Adjacency Matrix• Incidence Matrix• Adjacency List

Graph Theory

Page 36: graph theory

Adjacency, Incidence, and Degree

• Assume ei is an edge whose endpoints are (vj,vk)

• The vertices vj and vk are said to be adjacent

• The edge ei is said to be incident upon vj

• Degree of a vertex vk is the number of edges incident upon vk . It is denoted as d(vk)

Graph Theory S Sameen Fatima 37

eivj vk

Page 37: graph theory

Adjacency Matrix

• Let G = (V, E), |V| = n and |E|=m• The adjacency matrix of G written A(G),

is the |V| x |V| matrix in which entry ai,j is the number of edges in G with endpoints {vi, vj}.

Graph Theory S Sameen Fatima 38

a

b

c

de

w

x

y z

w x y z 0 1 1 0 1 0 2 0 1 2 0 1 0 0 1 0

wxyz

Page 38: graph theory

S Sameen Fatima 39

Adjacency Matrix• Let G = (V, E), |V| = n and |E|=m

• The adjacency matrix of G written A(G), is the |V| x |V| matrix in which entry ai,j is 1 if an edge exists otherwise it is 0

Graph Theory

1

5

2

4

3

1 2 3 4 5

12345

0 1 0 0 11 0 1 1 10 1 0 1 00 1 1 0 11 1 0 1 0

Page 39: graph theory

S Sameen Fatima 40

Adjacency Matrix (Weighted Graph)• Let G = (V, E), |V| = n and |E|=m

• The adjacency matrix of G written A(G), is the |V| x |V| matrix in which entry ai,j is weight of the edge if it exists otherwise it is 0

Graph Theory

1

5

2

4

3

1 2 3 4 5

12345

0 5 0 0 15 0 4 6 30 4 0 2 00 6 2 0 71 3 0 7 0

5

3

7

61

2

4

Page 40: graph theory

Incidence Matrix

• Let G = (V, E), |V| = n and |E|=m• The incidence matrix M(G) is the |V| x

|E| matrix in which entry mi,j is 1 if vi is an endpoint of ei and otherwise is 0.

Graph Theory S Sameen Fatima 41

a

b

c

d

e

w

x

yz

a b c d e 1 1 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 0 1

wxyz

Page 41: graph theory

S Sameen Fatima 42

Adjacency List Representation

• Adjacency-list representation – an array of |V | elements, one for each vertex in V– For each u V , ADJ [ u ] points to all its adjacent

vertices.

Graph Theory

Page 42: graph theory

S Sameen Fatima 43

Adjacency List Representation for a Digraph

Graph Theory

1

5

1

22

5

4 4

3 3

2 5

5 3 4

4

5

5

Page 43: graph theory

S Sameen Fatima 55

Minimum Spanning Tree

Graph Theory

Page 44: graph theory

S Sameen Fatima 56

Minimum Spanning Tree

• What is MST?

• Kruskal's Algorithm

• Prim's Algorithm

Graph Theory

Page 45: graph theory

S Sameen Fatima 57

A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree.

A graph may have many spanning trees.

or or or

Some Spanning Trees from Graph AGraph A

Spanning Trees

Graph Theory

Page 46: graph theory

S Sameen Fatima 58

All 16 of its Spanning TreesComplete Graph

Graph Theory

Page 47: graph theory

S Sameen Fatima 59

Minimum Spanning Trees

The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph.

5

7

2

1

3

4

2

1

3

Complete Graph Minimum Spanning Tree

Graph Theory

Page 48: graph theory

S Sameen Fatima 60

Kruskal's Algorithm

This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.

Graph Theory

Page 49: graph theory

S Sameen Fatima 61

The steps are:

1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together.

Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.

Graph Theory

Page 50: graph theory

S Sameen Fatima 62

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Complete Graph

Graph Theory

Page 51: graph theory

S Sameen Fatima 63

1

4

2

5

2

5

4

3

4

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

A AB D

B B

B

C D

J C

C

E

F

D

D H

J E G

F FG I

G GI J

H J JI

Graph Theory

Page 52: graph theory

S Sameen Fatima 64

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Sort Edges

(in reality they are placed in a priority queue - not sorted - but sorting them

makes the algorithm easier to visualize)

Graph Theory

Page 53: graph theory

S Sameen Fatima 65

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 54: graph theory

S Sameen Fatima 66

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 55: graph theory

S Sameen Fatima 67

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 56: graph theory

S Sameen Fatima 68

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 57: graph theory

S Sameen Fatima 69

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 58: graph theory

S Sameen Fatima 70

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Cycle

Don’t Add Edge

Graph Theory

Page 59: graph theory

S Sameen Fatima 71

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 60: graph theory

S Sameen Fatima 72

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 61: graph theory

S Sameen Fatima 73

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 62: graph theory

S Sameen Fatima 74

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Cycle

Don’t Add Edge

Graph Theory

Page 63: graph theory

S Sameen Fatima 75

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Graph Theory

Page 64: graph theory

S Sameen Fatima 76

4

1

2

2 1

3

32

4

A

B C

D

E F

G

HI

J

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Minimum Spanning Tree Complete Graph

Graph Theory

Page 65: graph theory

S Sameen Fatima 78

Prim's Algorithm

This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph, each time selecting the node whose connecting edge has the smallest weight out of the available nodes’ connecting edges.

Graph Theory

Page 66: graph theory

S Sameen Fatima 79

The steps are:

1. The new graph is constructed - with one node from the old graph. 2. While new graph has fewer than n nodes, 1. Find the node from the old graph with the smallest connecting

edge to the new graph, 2. Add it to the new graph

Every step will have joined one node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph.

Graph Theory

Page 67: graph theory

S Sameen Fatima 80

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Complete Graph

Graph Theory

Page 68: graph theory

S Sameen Fatima 81

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 69: graph theory

S Sameen Fatima 82

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 70: graph theory

S Sameen Fatima 83

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 71: graph theory

S Sameen Fatima 84

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 72: graph theory

S Sameen Fatima 85

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 73: graph theory

S Sameen Fatima 86

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 74: graph theory

S Sameen Fatima 87

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 75: graph theory

S Sameen Fatima 88

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 76: graph theory

S Sameen Fatima 89

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 77: graph theory

S Sameen Fatima 90

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 78: graph theory

S Sameen Fatima 91

4

1

2

2 1

3

32

4

A

B C

D

E F

G

HI

J

Complete Graph Minimum Spanning Tree

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Graph Theory

Page 79: graph theory

S Sameen Fatima 101

Examples

• Cost of wiring electronic components• Shortest route between two cities.• Shortest distance between all pairs of cities in

a road atlas.• Matching / Resource Allocation• Task scheduling

Graph Theory

Page 80: graph theory

S Sameen Fatima 102

Examples

• Flow of material – liquid flowing through pipes– current through electrical networks– information through communication networks– parts through an assembly line

• In Operating systems to model resource handling (deadlock problems)

• In compilers for parsing and optimizing the code.

Graph Theory

Page 81: graph theory

S Sameen Fatima 106

Graph Algorithms

Graph Theory

Page 82: graph theory

S Sameen Fatima 107

Search Algorithms

• Breadth First Search• Depth Dirst Search

Graph Theory

Page 83: graph theory

S Sameen Fatima 108

Breadth-First Search(BFS)

1. open (initial state).2. If open is empty , report failure , stop.3. s pop ( open )4. If s is a solution , report s, stop.5. succs successors(s).6. Add succs to tail of open.7. go to 2.

Graph Theory

Page 84: graph theory

S Sameen Fatima 109

Space for BFS

Space is calculated in terms of open list. In the worst case:The solution may be the rightmost node at the last levelAt the last level (level d) the no. of nodes = bd

Therefore, Total no: of nodes in the open list = bd

Space O (bd)

Graph Theory

Page 85: graph theory

S Sameen Fatima 110

Time for BFS

In the worst case, the solution will be the right most node at depth d, that is all the nodes would be expanded upto depth d.

No. of nodes processed at 1st level = 1No. of nodes processed at the 2nd level = bNo: of nodes processed at the 3rd level = b2

…………..No. of nodes processed at the dth level = bd

Therefore,Total no. of nodes processed = 1 + b + b2 +……..bd. = b ( bd – 1 ) (b – 1 ) O ( bd ) (ignoring 1 in comparison to b)

Graph Theory

Page 86: graph theory

S Sameen Fatima 111

Depth-First Search(DFS)

1. open ( initial state )2. If open is empty , report failure ,stop.3. s pop ( open )4. If s is a solution, report s, stop.5. succs successors (s).6. add succs to head of open7. go to 2.

Graph Theory

Page 87: graph theory

S Sameen Fatima 112

Space for DFS

Space is calculated in terms of open list. In the worst case: At the last level (level d) the no. of nodes = b At each of the preceding (d-1) levels i.e., 1, 2, 3, …., (d-1), the no. of nodes = b-1 Therefore, Total no: of nodes at the preceding (d-1) levels = (d-1)(b-1) Space b+ (d-1) ( b-1) b + db – d – b + 1 d ( b – 1) + 1 bd O ( d ) ( In terms of open list )

Graph Theory

Page 88: graph theory

S Sameen Fatima 113

Time for DFS

In the worst case, the solution will be the right most node at depth d, that is all the nodes would be expanded upto depth d.

No. of nodes processed at 1st level = 1No. of nodes processed at the 2nd level = bNo: of nodes processed at the 3rd level = b2

…………..No. of nodes processed at the dth level = bd

Therefore,Total no. of nodes processed = 1 + b + b2 +……..bd. = b ( bd – 1 ) (b – 1 ) O ( bd ) (ignoring 1 in comparison to b)

Graph Theory

Page 89: graph theory

S Sameen Fatima 114

Depth-First Iterative Deepening (DFID)

The depth-first iterative deepening algorithm combines the advantage of low space requirement of depth first search (DFS) and advantage of finding an optimal solution of the breadth first search (BFS)

time requirement, which is the same for both BFS and DFS 1. d 12. result depth first (initial state, d)3. (Comment: try to find a solution of length d using depth first search)4. If result ≠ NIL, report it, stop5. d d+ 16. go to 2

Graph Theory

Page 90: graph theory

S Sameen Fatima 115

Time requirement for DFID

On a search to depth d =1, b nodes are visited. For d = 2 , b1 + b2 nodes are visited and so on. At d = k, b1 + b2 + …….. + bk nodes are visited Let’s define the cost of DFID as a recurrence relation DFID(1) = b1DFID(k) = + DFID(k-1) This expands to

bk + bk-1 + bk-2 + ………………….. + b1

bk-1 + bk-2 + ………………….. + b1

bk-2 + ………………….. + b1

……

…….

……..

___________________________________________________________________

bk +2bk-1 +3bk-2 + ………………….. +kb1

= bk

For large k the above expression asymptotes to bk (as the expression in the parenthesis asymptotes to 1). Hence time required for DFID is O(bk)

Graph Theory

Page 91: graph theory

S Sameen Fatima 116

Examples

• Shortest route between two cities.• Shortest distance between all pairs of cities in

a road atlas.• Matching / Resource Allocation• Task scheduling• Cost of wiring electronic components• Visibility / Coverage

Graph Theory

Page 92: graph theory

S Sameen Fatima 117

Examples

• Flow of material – liquid flowing through pipes– current through electrical networks– information through communication networks– parts through an assembly line

• In Operating systems to model resource handling (deadlock problems)

• In compilers for parsing and optimizing the code.

Graph Theory

Page 93: graph theory

S Sameen Fatima 118

Thank you

Graph Theory