0 Up votes0 Down votes

1 views46 pagesGraph

Oct 13, 2015

© © All Rights Reserved

PPTX, PDF, TXT or read online from Scribd

Graph

© All Rights Reserved

1 views

Graph

© All Rights Reserved

- Ignou Mcs 031 Solved Paper
- Graphs
- graphs.pdf
- chap9b
- Temp
- Graph Search
- Tmp
- Lecture 15
- 5 GATE CS 2000 GeeksforGeeks
- Branch & Bound Global-Search Algorithm for Aircraft Ground
- Artificial Intelligence Chap2-2
- Design and Analysis of Algorithms.pdf
- 620001
- Cormen Algo-lec16
- uninformed_valla2
- 2012 Fq Cs 171 Final Exam Key
- SMU MCA Assignments
- Graph Terminology
- Compete-Guide, By Vishal Jain
- Slides2 - Copy

You are on page 1of 46

L S Vinh

Computer science department

Email: vinhls@vnu.edu.vn

Graphs

V is a set of nodes, called vertices

E is a collection of pairs of vertices, called edges

Vertices and edges are positions and store elements

Example:

A vertex represents an airport and stores the three-letter

airport code

An edge represents a flight route between two airports and

stores the mileage of the route

849

PVD

3

4

ORD

18

2

14

SFO

2555

337

HNL

LAX

43

7

1

1233

802

DFW

7

8

3

1

1120

LGA

10

9

MIA

2

Edge Types

Directed edge

ordered pair of vertices (u,v)

first vertex u is the origin

second vertex v is the

destination

e.g., a flight

Undirected edge

unordered pair of vertices (u,v)

e.g., a flight route

ORD

ORD

flight

AA 1206

849

miles

Directed graph

all the edges are directed

e.g., route network

Undirected graph

all the edges are undirected

e.g., flight network

3

PVD

PVD

Breadth-First Search

Breadth-first search

(BFS) is a general

technique for

traversing a graph

A BFS traversal of a

graph G

Visits all the vertices

and edges of G

Determines whether

G is connected

Computes the

connected

components of G

Computes a spanning

forest of G

n vertices and m

edges takes O(nm )

time

BFS can be further

extended to solve

other graph

problems

Find and report a path

with the minimum

number of edges

between two given

vertices

Find a simple cycle, if

there is 4one

Depth-First Search

Depth-first search

(DFS) is a general

technique for

traversing a graph

A DFS traversal of a

graph G

Visits all the vertices

and edges of G

Determines whether

G is connected

Computes the

connected

components of G

Computes a spanning

forest of G

n vertices and m

edges takes O(nm )

time

DFS can be further

extended to solve

other graph

problems

Find and report a path

between two given

vertices

Find a cycle in the

graph

Depth-first search is

to graphs what Euler

tour is to binary

5

trees

Topological Sorting

Number vertices, so that (u,v) in E implies

u < v wake up 1

A typical student day

2

study computer sci.

7

play

nap

eat

4

8

write c.s. program

9

make cookies

for professors

10

sleep

5

more c.s.

6

work out

11

dream about graphs

6

Sorting

Method TopologicalSort(G)

H G // Temporary copy of G

n G.numVertices()

while H is not empty do

Let v be a vertex with no outgoing edges

Label v n

nn-1

Remove v from H

7

Topological Sorting

Algorithm using DFS

using depth-first search

Algorithm topologicalDFS(G)

Input dag G

Output topological ordering of

G

n G.numVertices()

for all u G.vertices()

setLabel(u, UNEXPLORED)

for all e G.edges()

setLabel(e, UNEXPLORED)

for all v G.vertices()

if getLabel(v)

UNEXPLORED

v)

O(n+m)topologicalDFS(G,

time.

Algorithm topologicalDFS(G, v)

Input graph G and a start vertex v of

G

Output labeling of the vertices of G

in the connected component of

v

setLabel(v, VISITED)

for all e G.incidentEdges(v)

if getLabel(e) UNEXPLORED

w opposite(v,e)

if getLabel(w)

UNEXPLORED

setLabel(e,

DISCOVERY)

topologicalDFS(G, w)

Label v with topological number n

nn-1

8

Topological Sorting

Example

Topological Sorting

Example

9

10

Topological Sorting

Example

8

9

11

Topological Sorting

Example

7

8

9

12

Topological Sorting

Example

7

8

9

13

Topological Sorting

Example

7

8

9

14

Topological Sorting

Example

4

6

7

8

9

15

Topological Sorting

Example

3

4

6

7

8

9

16

Topological Sorting

Example

2

3

4

6

7

8

9

17

Topological Sorting

Example

2

3

4

6

7

8

9

18

Shortest Paths

A

8

B

2

2

7

5

E

C

3

4

1

D

8

Weighted Graphs

numerical value, called the weight of the edge

Edge weights may represent, distances, costs, etc.

Example:

In a flight route graph, the weight of an edge represents

the distance in miles between the endpoint airports

LAX

1233

DFW

849

14

7

138

1120

PVD

1205

337

HNL

2555

3

4

17

ORD

802

SFO

1843

LGA

10

9

MIA

20

Shortest Paths

want to find a path of minimum total weight between u

and v.

Length of a path is the sum of the weights of its edges.

Example:

Shortest path between Providence and Honolulu

Applications

Internet packet routing

Flight reservations

Driving directions

1843

SFO

LAX

1233

DFW

Phm Bo Sn - DSA

849

14

7

8

3

1

1120

PVD

1205

Dec 2008

337

HNL

2555

43

7

1

ORD

802

LGA

10

9

MIA

21

Dijkstra Algorithm

Given G=(V, E), where weight (u,v) > 0

is the weight of edge (u, v) E. Find

the shortest path from s to e.

General idea

- d[v]: the shortest distance from s to v.

- pre[v] = u: the previous vertex of v

on

the shortest path from s to

v.

Relaxation (s, u, v):

If d [u] + weight (u, v) < d [v]

d [v] = d [u] + weight (u, v)

pre[v] = u

suv

d(u) 50

s

d(u) 50

s

10 d(z) 75

10 d(z) 60

Dijkstra algorithm

Known = {the set of vertices which the shortest paths are known}

Unknown = {the set of vertices which the shortest paths are unknown}

Repeat

Find u unknown with the smallest d[u]

if u = e then

done

else {

known = known {u}

unknown = unknown {u}

for each edge (u, v):

relaxation (s, u, v)

}

Dijkstra Algorithm {

Known = {s};

Unknown = {E} {s}

for v unknown {

d[v] = weight [s, v];

pre[v] = s;

}

u = s; // at the first step

while (u != e ){

Select u such that d[u] = min {d[x] | x unknown}

Known = Known {u}

Unknown = Unknown {u}

for v Unknown

if d[u] + weight [u, v] < d[v] {

//relax (s, u, v)

d[v] = d[u] + weight[u,v];

pre[v] = u;

}

}

path = {e}

while (e != s) {

e = pre[e]

path = {e} path

}

}

Example

A

8

B

2

7

C

3

9

F

2

7

5

E

C

3

B

2

D

8

2

7

C

3

4

1

D

8

F

25

5

E

8

D

11

4

1

8

D

5

E

8

B

Example (cont.)

A

8

B

2

C

3

5

E

4

1

D

8

5

A

8

B

2

C

3

4

1

8

F

26

D

5

8

B

2

C

0

5

E

-8

D

9

already in the cloud with

d(C)=5!

27

Bellman-Ford

Algorithm

Algorithm BellmanFord(G, s)

negative-weight edges

for all v G.vertices()

if v s

Must assume directed

setDistance(v, 0)

edges (for otherwise we

else

would have negativesetDistance(v, )

weight cycles)

for i 1 to n-1 do

Iteration i finds all

for each e G.edges()

shortest paths that use i

{ relax edge e }

edges.

u G.origin(e)

Running time: O(nm).

z G.opposite(u,e)

r getDistance(u) weight(e)

Can be extended to

if r getDistance(z)

detect a negativesetDistance(z,r)

weight cycle if it exists

How?

28

Bellman-Ford Example

Nodes are labeled with their d(v) values

0

-2

7

-2

8

8

-2

7

3

5

-2

7

1

-2

-2

1

-2

3

-2

-2

5 8

-1

7

3

-2

-2

9

1

29

4

9

-1

5

Spanning subgraph

Subgraph of a graph G

containing all the vertices

of G

Spanning tree

Spanning subgraph that is

itself a (free) tree

Spanning tree of a

weighted graph with

minimum total edge

weight

Applications

ORD

10

PIT

DEN

9

3

DCA

STL

8

DFW

2

ATL

Communications networks

Transportation networks

31

Cycle Property

Cycle Property:

Let T be a minimum

spanning tree of a

weighted graph G

Let e be an edge of G

that is not in T and let

C be the cycle formed

by e with T

For every edge f of C,

weight(f) weight(e)

Proof:

By contradiction

If weight(f) weight(e) we

can get a spanning

tree of smaller weight

by replacing e with f

C

2

7

Replacing f with e yields

a better spanning tree

f

8

4

C

2

3

8

7

32

Partition Property

U

Partition Property:

Consider a partition of the vertices of

G into subsets U and V

Let e be an edge of minimum weight

across the partition

There is a minimum spanning tree of

G containing edge e

Proof:

Let T be an MST of G

If T does not contain e, consider the

cycle C formed by e with T and let f

be an edge of C across the partition

By the cycle property,

weight(f) weight(e)

Thus, weight(f) weight(e)

We obtain another MST by replacing

f with e

4

9

8

8

e

7

another MST

U

4

9

8

8

e

7

33

Kruskals Algorithm

A priority queue

stores the edges

outside the cloud

Key: weight

Element: edge

algorithm

cloud that

encompasses the

MST

A tree T which is our

MST

Algorithm KruskalMST(G)

for each vertex V in G do

define a Cloud(v) of {v}

let Q be a priority queue.

Insert all edges into Q using their

weights as the key

T

while T has fewer than n-1 edges do

edge e = T.removeMin()

Let u, v be the endpoints of e

if Cloud(v) Cloud(u) then

Add edge e to T

Merge Cloud(v) and Cloud(u)

return T

34

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

35

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

36

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

37

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

38

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

39

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

40

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

41

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

42

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

43

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

44

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

45

Example

2704

BOS

867

849

ORD

1846

LAX

1391

1464

1235

187

144

JFK

1258

184

802

SFO

337

740

621

PVD

BWI

1090

DFW

946

1121

MIA

2342

Dec 2008

Phm Bo Sn - DSA

46

- Ignou Mcs 031 Solved PaperUploaded byGaurav Sood
- GraphsUploaded byRahulRoy
- graphs.pdfUploaded byJayant Priyadarshi
- chap9bUploaded byHarshitShukla
- TempUploaded bySivakumar Ravichandran
- Graph SearchUploaded byAhmed Said
- TmpUploaded bySotiBriti
- Lecture 15Uploaded byYashasaavi Ladha
- 5 GATE CS 2000 GeeksforGeeksUploaded byjoshna1234
- Branch & Bound Global-Search Algorithm for Aircraft GroundUploaded bythanhvan34
- Artificial Intelligence Chap2-2Uploaded byJesse Sanders
- Design and Analysis of Algorithms.pdfUploaded byDesire Frk
- 620001Uploaded byRajput Sunny
- Cormen Algo-lec16Uploaded bygeniusamit
- uninformed_valla2Uploaded byfoobaer
- 2012 Fq Cs 171 Final Exam KeyUploaded byAhmed Ellkholy
- SMU MCA AssignmentsUploaded byNarendra Dev
- Graph TerminologyUploaded byJunaid khan
- Compete-Guide, By Vishal JainUploaded byapi-3782519
- Slides2 - CopyUploaded byJuhiRai
- ThurstonLectNotesUploaded bynebisnebis
- BookUploaded byAshraf Iqbal
- zipcfgUploaded byGilsonCosta
- workUploaded byapi-20011468
- asdsdasfsfUploaded byElbiz Alata
- fgfdhdfhUploaded byElbiz Alata
- sdsdsdsfUploaded byElbiz Alata
- sdsdsdsdfvUploaded byElbiz Alata
- sadsassfafdccdUploaded byElbiz Alata
- dfsdfdUploaded byElbiz Alata

- ADA labUploaded byVikram Rao
- feigeUploaded byjohnoftheroad
- 1 Chandler 18.304lecture1Uploaded byFelipe Torres Silva
- cs261 hw3Uploaded bytony1337
- Euler i an Path and CircuitUploaded byRifkyardhana Kisnosaputra
- Krazydad Kakuro Puzzles 8x8Uploaded byJim Bumgardner
- graph2Uploaded byHaroon Ashraf
- kakuroUploaded byChamithaSamantha
- Dijkstra's AlgorithmUploaded byAdam Meyers
- Matching (Graph Theory)Uploaded byMohit Gupta
- Network FlowUploaded byPratik Mehta
- Xl SudokuUploaded byruytguerra
- Sudoku Print Version_116Uploaded byigooooo
- Chapter9 Graph AlgorithmUploaded byJatin Chaudhari
- Lecture 17 - Minimum Spanning Tree.pdfUploaded byshubhamgupta007
- A starUploaded bytoncuvasile
- Network Lecture Note 1Uploaded bySumber Unduh
- 8 Sudokus Medium 2pp SolutionUploaded byNSee5
- UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdfUploaded byViney Gupta
- 63913890104Uploaded bySaeid Myron
- AlgoUploaded byPrasenjit Das
- Assignment 2 Mod 3 - SolutionUploaded byRohitGoswami
- Network FlowUploaded byashrafcheru
- Medium Sudoku - 50 Printable Puzzles With AnswersUploaded byVeronicaGelfgren
- Week05 Graph 1Uploaded byEdison Mamani Ramirez
- Uniform Cost SearchUploaded byScribdfav
- algo8Uploaded byBijal Patel
- Minimum Spanning TreeUploaded byNeha Verma
- 12_sudokus_medium_18.2.2016-00..57.pdfUploaded byclient_escopy
- H20 SolutionUploaded bycherryhitech

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.