graphs. graphs many interesting situations can be modeled by a graph. many interesting situations...

33
Graphs Graphs

Post on 20-Dec-2015

225 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

GraphsGraphs

Page 2: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

GraphsGraphs• Many interesting situations can be modeled by a graph.Many interesting situations can be modeled by a graph.

Ex. Mass transportation system, computer network.Ex. Mass transportation system, computer network.

Page 3: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Terminology

A graph consists of a set of nodes (vertices) and a set of edges.

An edge connects two distinct nodes (vertices).

A node is basic component, which usually contains some information.

A subgraph is graph which consists of a subset of nodes (vertices) and a subset of edges of a graph.

Page 4: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

A path is a sequence of nodes such that each successive pair is connected by an edge.

A path is a simple path if each of its nodes occurs once in the sequence.

A cycle is path that is a simple path except that the first and last nodes are the same.

A path

Simple path

cycle

Page 5: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

A graph is a connected graph if there is a path between every pair of its nodes.

Connected graph

Not a connected graph

Page 6: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Two nodes are adjacent nodes if there is an edge that connects them.

Neighbors of a node are all nodes that are adjacent to it. A Tree is the special case of a graph that

(i) is connected

(ii) has no cycle

Page 7: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

If a connected graph has n nodes and n-1 If a connected graph has n nodes and n-1 edges, then it is a tree. This tree is called edges, then it is a tree. This tree is called Spanning TreeSpanning Tree..

AnAn oriented tree oriented tree is a tree in which one is a tree in which one node has been designated the root node. node has been designated the root node.

Page 8: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

A A directed graphdirected graph or or digraphdigraph is a graph in which is a graph in which each edge has an associated direction.each edge has an associated direction.

A A weighted graphweighted graph is a graph in which each edge is a graph in which each edge has an associated value.has an associated value.

12

22234

32

5

5217

25 1623

274519

24

Page 9: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Specification of GraphSpecification of Graph

Elements: Elements: A graph consists of nodes and edgesA graph consists of nodes and edges

Structure: Structure: An edge is a one-to-one relationship between a An edge is a one-to-one relationship between a pair pair of distinct nodes. A pair of nodes can be of distinct nodes. A pair of nodes can be connected connected by at most one edge, but any node can be by at most one edge, but any node can be connected connected to any collection of other to any collection of other nodes.nodes.

Domain: Domain: The number of nodes (vertices) in a graph is bounded.The number of nodes (vertices) in a graph is bounded.

OperationsOperations::

InsertNode InsertNode (( Type e Type e))

Requires: Requires: GG is not full. is not full.

Results:Results: If If GG does not contain an element whose key value is does not contain an element whose key value is e.key then e.key then ee is inserted in is inserted in GG and inserted is true, and inserted is true, otherwise inserted is false.otherwise inserted is false.

Page 10: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

InsertEdge InsertEdge ((Key k1, k2Key k1, k2))Requires: Requires: GG is not full and k1!=k2. is not full and k1!=k2.Results:Results: If If GG contains two nodes whose key values are k1 and k2 contains two nodes whose key values are k1 and k2

then then GG contains an edge connecting those nodes. If the two contains an edge connecting those nodes. If the two nodes were connected by an edge before operation nodes were connected by an edge before operation InsertEdge then inserted is false; otherwise inserted is true.InsertEdge then inserted is false; otherwise inserted is true.

DeleteNode DeleteNode ((Key kKey k))Results:Results: GG does not contain an element whose key value is k. if does not contain an element whose key value is k. if GG

contained a node before this operation with key value k contained a node before this operation with key value k then deleted is true and no edge that connected this node to then deleted is true and no edge that connected this node to an other node is in G; otherwise deleted is false.an other node is in G; otherwise deleted is false.

DeleteEdge DeleteEdge ((Graph g, Key k1, k2Graph g, Key k1, k2))

Results:Results: GG does not contain an edge that connects nodes whose does not contain an edge that connects nodes whose key values are k1 and k2. If key values are k1 and k2. If GG contained such an edge contained such an edge before this operation then deleted is true; otherwise before this operation then deleted is true; otherwise deleted is false.deleted is false.

Page 11: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Update Update ((T eT e))ResultsResults:: If If GG contained a node with key value e.key then the contained a node with key value e.key then the

element in the node is e and updated is true. Otherwise element in the node is e and updated is true. Otherwise updated is false.updated is false.

Retrieve Retrieve ((key k, T ekey k, T e))ResultsResults:: If If GG contains a node whose key value is k before this contains a node whose key value is k before this

operation then e is that element and retrieved is true; operation then e is that element and retrieved is true; otherwise retrieved is false.otherwise retrieved is false.

Full Full ()()ResultsResults:: If If GG is full then Full returns true; otherwise Full returns is full then Full returns true; otherwise Full returns

false.false.

Page 12: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Representation of a GraphRepresentation of a Graph

There are two approaches to representing graphsThere are two approaches to representing graphs

((ii)) Adjacency MatrixAdjacency Matrix

((iiii)) Adjacency ListAdjacency List

Page 13: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

This representation is very simple, but the space requirement is This representation is very simple, but the space requirement is O(nO(n22) if the number of vertices is n.) if the number of vertices is n.

This representation is suitable only if the graph is dense with the This representation is suitable only if the graph is dense with the number of edges i.e. Onumber of edges i.e. O((nn22)), n being the number of vertices. But in , n being the number of vertices. But in most of the applications this is not true.most of the applications this is not true.

Adjacency matrixAdjacency matrix

A two dimensional array whose components are of type Boolean A two dimensional array whose components are of type Boolean and whose index values correspond to the nodes.and whose index values correspond to the nodes.

55

1122

33

44

EX. EX. 11 22 33 44 55

11 00 11 11 00 11

22 11 00 11 11 00

33 11 11 00 11 11

44 00 11 11 00 11

55 11 00 11 11 00

If A is the adjacency If A is the adjacency matrix matrix corresponding to a corresponding to a graph graph GG, then , then AAijij

corresponds to the corresponds to the edge edge ((i, ji, j)), and is , and is true if there is an true if there is an edge between the edge between the vertices i, and j.vertices i, and j.

Page 14: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Adjacency listAdjacency listIt is an array where each cell corresponds to a vertex of a It is an array where each cell corresponds to a vertex of a

graph and stores the header of a list of all adjacent vertices. graph and stores the header of a list of all adjacent vertices.

12

3

4 5

1

2

3

4

5

NULL

NULL

NULL

NULL

NULL

1

1

1

5

5

5

3

3

3

3

2

2

2

4

4

4

Ex. The following is an adjacency list corresponding to the graph on the Ex. The following is an adjacency list corresponding to the graph on the right.right.

This is an ideal representation if the graph is not dense. This is an ideal representation if the graph is not dense.

In this case the space requirement is O(e + n) where e is In this case the space requirement is O(e + n) where e is the number of edges and n is the number of vertices. the number of edges and n is the number of vertices.

Page 15: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Traversal of a GraphTraversal of a Graph

There are two methods for graph traversalThere are two methods for graph traversal

((ii)) Breadth First SearchBreadth First Search

((iiii)) Depth First SearchDepth First Search

Process each node of the graph only once Process each node of the graph only once

Page 16: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Breadth First Search (BFS)Breadth First Search (BFS)

Start at some source node Start at some source node ss and visit all its neighbors, and visit all its neighbors, then visit the neighbors of each neighbor of s and so then visit the neighbors of each neighbor of s and so

on.on.

1.1. Assign the status of Assign the status of waitingwaiting to all nodes of a graph. to all nodes of a graph.

2.2. Start with source node s, and put it in queueStart with source node s, and put it in queue

3.3. Dequeue one node from the queue, assign it the status of Dequeue one node from the queue, assign it the status of processedprocessed, and assign its neighbors the status of , and assign its neighbors the status of readyready and put them in the queue.and put them in the queue.

4.4. Repeat Step 3 until the queue is empty.Repeat Step 3 until the queue is empty.

ALgorithmALgorithm

Page 17: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

ExampleExample

Consider a graph Consider a graph with nodes with nodes a, b, c, d, a, b, c, d, e, f, g, se, f, g, s

Page 18: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Pseudocode for BFSPseudocode for BFSBFS(G, s)BFS(G, s){{

for each node u of Gfor each node u of Gset status[u] = waiting;set status[u] = waiting;

status[s] = ready;status[s] = ready;Q = {s};Q = {s};

while( Q.IsEmpty)while( Q.IsEmpty){{

u = Q.dequeue();u = Q.dequeue();for each v in Neigh[u]for each v in Neigh[u]{{

if(status[v] = waiting)if(status[v] = waiting){{ staus[v] = ready;staus[v] = ready; Q.enqueue(v);Q.enqueue(v);}}

}}status[u] = processed;status[u] = processed;

}}}}

Page 19: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Depth First Search (DFS)Depth First Search (DFS)

DFS visits one node and then visits one of its neighbors DFS visits one node and then visits one of its neighbors and puts the rest of its neighbors into a stack and so on.and puts the rest of its neighbors into a stack and so on.

1.1. Assign the status of Assign the status of waitingwaiting to all nodes of a graph. to all nodes of a graph.

2.2. For each node of the graph, follow the following steps, For each node of the graph, follow the following steps,

a)a) Change the status of the node to be Change the status of the node to be readyready and put in a and put in a stackstack

b)b) Repeat the following steps until the stack is emptyRepeat the following steps until the stack is empty

c)c) Get a node from the stack, Get a node from the stack, processprocess it, change its status it, change its status to to processedprocessed and change the status from waiting to and change the status from waiting to ready of its neighbors and put them in the stackready of its neighbors and put them in the stack

d)d) Go to step bGo to step b

Page 20: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

a

s

d

b

ce

f

g

S: sa

s

d

b

ce

f

g

S: a c ds

a

s

d

b

ce

f

g

S: a c bd a

s

d

ce

f

g

S: a c gb

b

Page 21: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

a

s

d

ce

f

g

S: a c gb

b

a

s

d

ce

f

g

S: a c f eg

b

a

s

d

ce

f

g

S:e f c a

b

Page 22: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Pseudocode for DFSPseudocode for DFSBFS(G)BFS(G){{

for each node u of Gfor each node u of Gstatus[u] = waiting;status[u] = waiting;

for each node u of Gfor each node u of Gif(status[u] == waiting)if(status[u] == waiting)

Visit(u)Visit(u)}}Visit(u)Visit(u){{

stack S;stack S;status[u] = ready;status[u] = ready;S.push(u)S.push(u)

while( S.IsEmpty)while( S.IsEmpty){{

v = S.pop();v = S.pop();for each w in Neigh[v]for each w in Neigh[v]{{

if(status[w] = waiting)if(status[w] = waiting){{ staus[w] = ready;staus[w] = ready; S.push(w);S.push(w);}}

}}status[v] = processed;status[v] = processed;

}}}}

Page 23: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Shortest Path Shortest Path ((unweightedunweighted))

The problemThe problem:: Find the shortest path from a vertex v to Find the shortest path from a vertex v to every other vertex in a graph every other vertex in a graph GG

The unweighted path measures the number of edges, The unweighted path measures the number of edges, ignoring the edge’s weights ignoring the edge’s weights ((if anyif any))

Page 24: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Finding Shortest Unweighted PathFinding Shortest Unweighted Path((Simple AlgorithmSimple Algorithm))

11 Mark all vertices with Mark all vertices with ddvv = infinity = infinity

22 Select a starting vertex s, and set dSelect a starting vertex s, and set dss = 0, and set = 0, and set

shortestshortest = 0 = 0

33 For all vertices v with dFor all vertices v with dvv = shortest, scan their = shortest, scan their

adjacency lists for vertices w where dadjacency lists for vertices w where dww is infinity is infinity

– For each such vertex w, set dFor each such vertex w, set dw w to shortest+1to shortest+1

44 Increment shortest and repeat step Increment shortest and repeat step 33, until there are no , until there are no vertices wvertices w

For a vertex v, dFor a vertex v, dv v is the distance between a starting vertex and vis the distance between a starting vertex and v

Page 25: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Simple algorithm: exampleSimple algorithm: example

v2 v3 v4

v0 v1

v5 v6

0

v2 v3 v4

v0 v1

v5 v6

0

1

1

v2 v3 v4

v0 v1

v5 v6

0

1 2

2

1

v2 v3 v4

v0 v1

v5 v6

0

1 2

2

1 3

3

Page 26: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Simple algorithm: using queuesSimple algorithm: using queues In step In step 33 of the simple algorithm, we search through of the simple algorithm, we search through the the

entire graph for vertices v with dentire graph for vertices v with dvv = shortest = shortest Instead, because we deal with exactly these vertices in Instead, because we deal with exactly these vertices in

the previous iteration of step the previous iteration of step 33, we can queue them, we can queue them

v2 v3 v4

v0 v1

v5 v6

0

1

1

Queue: v0, v5

v2 v3 v4

v0 v1

v5 v6

0

1 2

2

1

Queue: v5, v1, v3

Page 27: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Shortest weighted path:Shortest weighted path:Dijkstra’s Dijkstra’s ((Dike-StruhDike-Struh) algorithmalgorithm

What is the lowest-cost path between What is the lowest-cost path between two two vertices in a weighted graph?vertices in a weighted graph?

Only works on graphs all of whose Only works on graphs all of whose weights weights are positiveare positive

Works in a similar way to the unweighted Works in a similar way to the unweighted algorithm, except we possibly adjust the algorithm, except we possibly adjust the values in the vertices more than oncevalues in the vertices more than once

Page 28: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Dijkstra’s Dijkstra’s ((Dike-StruhDike-Struh) AlgorithmAlgorithm

NOTENOTE:: We can use an ‘inverse’ priority queue, in order We can use an ‘inverse’ priority queue, in order to select the vertex with lowest costto select the vertex with lowest cost Algorithm based on breadth-first traversalAlgorithm based on breadth-first traversal

Initialise all costs to infinityInitialise all costs to infinitySelect a start vertexSelect a start vertexcurrent_v = start vertexcurrent_v = start vertexRepeatRepeat For every adjacent vertex For every adjacent vertex vv of of current_vcurrent_v If If ((cost of cost of current_vcurrent_v + weight of edge to + weight of edge to vv)) < cost of < cost of vv Reduce cost of Reduce cost of vv to to ((cost of cost of current_vcurrent_v + weight of edge + weight of edge vv)) Select lowest cost vertex as Select lowest cost vertex as current_vcurrent_v and examine adjacent vertices and examine adjacent verticesUntil all vertices have been processedUntil all vertices have been processed

Page 29: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

ExampleExample

v2 v3 v4

v0 v1

v5 v6

0

4

21

2

10

5

1

6

3

28

4

Queue: v0

v2 v3 v4

v0 v1

v5 v6

0 2

1

4

21

2

10

5

1

6

3

28

4

Queue: v3, v1

Page 30: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

v2 v3 v4

v0 v1

v5 v6

3

0 2

1

9 5

34

21

2

10

5

1

6

3

28

4

Queue: v1, v4, v2, v6, v5

v2 v3 v4

v0 v1

v5 v6

3

0 2

1

9 5

34

21

2

10

5

1

6

3

28

4

Queue: v4, v2, v6, v5

Page 31: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

v2 v3 v4

v0 v1

v5 v6

3

0 2

1

9 5

34

21

2

10

5

1

6

3

28

4

Queue: v2, v6, v5

v2 v3 v4

v0 v1

v5 v6

3

0 2

1

8 5

34

21

2

10

5

1

6

3

28

4

Queue: v6, v5

Page 32: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

v2 v3 v4

v0 v1

v5 v6

3

0 2

1

6 5

34

21

2

10

5

1

6

3

28

4

Queue: v5

v2 v3 v4

v0 v1

v5 v6

3

0 2

1

6 5

34

21

2

10

5

1

6

3

28

4

Queue:

The shortest path from vThe shortest path from v00 to v to v66 is v is v00vv33vv66 and the shortest path from v and the shortest path from v00 to v to v44 is v is v00vv33vv44

Page 33: Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Complexity of Dijkstra’s algorithm

• The time complexity is O(|V|2 + |E|)

• For dense graphs where |E| = O(|V|2), the algorithm is optimal and the time complexity is O(|V|2)

• For sparse graphs, it can be improved to give a time complexity of O(|E| log |V|)