chapter seven graphs
Post on 06-Apr-2018
224 Views
Preview:
TRANSCRIPT
-
8/3/2019 Chapter Seven Graphs
1/44
Click to edit Master subtitle style5/3/12
Chapter Seven:
trees & graphs
-
8/3/2019 Chapter Seven Graphs
2/44
Click to edit Master subtitle style5/3/12
Graphs
-
8/3/2019 Chapter Seven Graphs
3/44
5/3/12
What is Graphs?
Agraph is a mathematical abstraction used to represent"connectivity information".
A graph G consists of a set of V or vertices (nodes) and a set ifedges (arcs).
We write G=(V,E).
V is a finite non empty set of vertices.
E is a set of pairs of vertices. These pairs are called edges
-
8/3/2019 Chapter Seven Graphs
4/44
5/3/12
What is Graphs...
An edge e = (v,w), is a pair of vertices v and w, and is said to be incidentwith v and w.
V(G)= { 1,2,3,4,5}
E(G)={ (1,2), (2,3), (3,4), (4,5), (1,5), (1,3), (3,5) }
1
52
3 4
-
8/3/2019 Chapter Seven Graphs
5/44
5/3/12
Type of Graph
There is two type of graphs
1. undirected graph.
. In an undirected graph, pair of vertices representing any edge
is unordered.. Thus (v,w) and (w,v) represent the same edge.
. V(G)= { 1,2,3,4,5}
.
E(G)={ (1,2), (2,3), (3,4), (4,5), (1,5), (1,3), (3,5) }.
1
52
3 4
-
8/3/2019 Chapter Seven Graphs
6/44
5/3/12
Type of Graph
2. A Directed Graph
Each edge is an ordered pairs of vertices, i.e. each edge isrepresented by a directed pair.
If e = (v,w) then v is tail or initial vertex and w is head of finalvertex.
Subsequently (v,w) and (w,v) represent two different edges.
The direction is indicated by arrow.
V(G) = {1,2,3,4,5}
E(G) = { (1,2), (2,3), (3,4), (5,3), (5,4), (5,1) }
1
52
3 4
-
8/3/2019 Chapter Seven Graphs
7/44
5/3/12
Graphs
Adjacent Vertices
Vertex v1 is said to be adjacent to vertex v2 if there is an edge (v1,v2) or (v2, v1).
Ex:
Vertices adjacent to node 3 are 1,5,6,4
Vertices adjacent to node 2 are 1 and 7
Finite Graph:
1
23
7
6 45
-
8/3/2019 Chapter Seven Graphs
8/44
5/3/12
Graphs
Loop:
An edge with identical end points is called a loop.
Multiple edges:
The edges connected the same end points.
Multi-Graph:
A graph with multiple edges. For multi graphs, even though, thereare finite number of nodes, the edges may not be finite.
-
8/3/2019 Chapter Seven Graphs
9/44
5/3/12
Graphs
Path:
A path from vertex v to vertex w is a sequence of vertices, eachadjacent to the next.
1,3,4 is a path1,3,6 is a path
1,2,7 is a path
1,3,4,7 is a path
We may notice that there is a path which starts at vertex 1 andfinishes at vertex 1. i.e. path 1,3,4,7,2,1. Such a path is called acycle.
Cycle:- A cycle is a path in which first and last vertices are thesame.
1
23
7
6 45
-
8/3/2019 Chapter Seven Graphs
10/44
5/3/12
Graphs
Weighted graphs:
Sometimes, we include a "weight" (number) with each edge.
Weight can signify length (for a geometric application) or
"importance".
-
8/3/2019 Chapter Seven Graphs
11/44
5/3/12
Graphs
Connected Graph
A graph is called 'connected' , if there exists a path betweenany two of its nodes. The above graph is a connected graph.
A weakly connected graph
54
1
2
3
-
8/3/2019 Chapter Seven Graphs
12/44
5/3/12
Graphs
Strongly Connected
A digraph is called strongly connected if there is a directed pathfrom any vertex to any other vertex.
54
1
2
3
Strongly Connected Graph
-
8/3/2019 Chapter Seven Graphs
13/44
5/3/12
Graphs
In a digraph, the path is called a directed pathand a cycle iscalled as directed cycle.
1,2 is a directed path
1,3,5,7,6 is a directed path
1,4,5 is not a directed path There is no directed cycle in the above graph.
Simple Graph: is a graph with the following properties:
1
32
4
5
6
7
-
8/3/2019 Chapter Seven Graphs
14/44
5/3/12
Graphs
Degree
There is no limitation of number of edges incident on one vertex.
It could be none, one or more.
The number of edges incident on a vertex determines its degree.
The number of edges containing a node is called the degree ofthat node.
45 6
1
2
7
3
-
8/3/2019 Chapter Seven Graphs
15/44
5/3/12
Graphs
In a digraph we attach an indegree and an outdegree to each ofthe vetice.
Indegree:
The Indegree of a node 'u' is the number of edges ending at 'u' . Outdegree:
The outdegree of a node 'u' is the number of edges starting from 'u'.
The indegree of vertex 5 is 2
The Outdegree of vertex 5 is 1 54
1
2
3
-
8/3/2019 Chapter Seven Graphs
16/44
5/3/12
Graphs
Tree Graph: It is a special type of graph.
It is connected and
There are no cycles in the graph.1
65
2
436
10
98
7
A
B
C
E
D
3
76
8
9
-
8/3/2019 Chapter Seven Graphs
17/44
5/3/12
Application of Graphs
History:
Maze-searching.
Euler's crossing problem: the Konigsberg bridges
-
8/3/2019 Chapter Seven Graphs
18/44
5/3/12
Application of Graphs
Applications:
Fundamental mathematical construct to represent"connectivity".
Appears in thousands of problems.
Source of many classic problems: travelingsalesman, routing, spanning trees.
Many "graph-structured" applications: networks,transportation-systems, electronic circuits,molecules.
Graphs are the essential data structure in networka lications.
-
8/3/2019 Chapter Seven Graphs
19/44
5/3/12
Graph Representation
The picture with circles (vertices) and lines (edges) is only adepiction=> a graph is purely a mathematical abstraction.
Graph is a mathematical structure and finds its application in
many areas of interest in which problems need to be solvedusing computers.
Thus this mathematical structure must be represented as somekind of data structures.
Two such representations are commonly used. They are,
Adjacent matrix and
Adjacency list representation
The choice of representation depends on the application and
-
8/3/2019 Chapter Seven Graphs
20/44
5/3/12
Graph Representation ...Adjacency Matrix
The adjacency matrix A for graph G = (V,E) with n vertices, isan nXn matrix of bits, such that,
Aij = 1 if there is an edge from vi to vj and
Aij = 0 if there is no such edgeVertice 1 2 3 4 5
1 0 1 1 0 1
2 1 0 1 0 0
3 1 1 0 1 1
4 0 0 1 0 1
5 1 0 1 1 0
1
52
3 4
-
8/3/2019 Chapter Seven Graphs
21/44
5/3/12
Graph Representation ...Adjacency Matrix
The total number of 1s account for the number of edges in thedigraph.
The number of 1s in each row tells the out degree of thecorresponding vertex.
The total number of 1's in each column tells the in degree of the
1
32
4
5
6
7
Vertex 1 2 3 4 5 6 7
1 0 1 1 1 0 0 02 0 0 0 0 0 0 03 0 0 0 0 1 0 04 0 0 0 0 0 1 0
5 0 0 0 1 0 0 16 0 0 0 0 0 0 07 0 0 0 0 0 1 0
-
8/3/2019 Chapter Seven Graphs
22/44
5/3/12
Graph Representation ...Adjacency List Representation
we store a graph as a linked structure.
We store all the vertices in a list and then for each vertex, wehave a linked list of its adjacent vertices.
v1
V3V2
V4
V5
V6
V1
V2
V3
V4
V5
V6
V2
V3
V4
V1
V4
V6V5
V3
-
8/3/2019 Chapter Seven Graphs
23/44
5/3/12
Graph Representation ...Adjacency List Representation
-
8/3/2019 Chapter Seven Graphs
24/44
5/3/12
Graph Traversal
A graph traversal means visiting all the nodes of the graphexactly once.
Two graph traversal methods are commonly used. These are,
Depth First Search (DFS) Breadth First Search (BFS)
-
8/3/2019 Chapter Seven Graphs
25/44
5/3/12
Graph Traversal Depth First Search
In graphs, we do not have any start vertex or any special vertexsignaled out to start traversal from.
Therefore the traversal may start from any arbitrary vertex.
We start with vertex v. An adjacent vertex is selected and a depth first search is initiated
from it.
i.e. V1,V2,.Vk are adjacent vertices to vertex v.
We may select any vertex from this list.
Say we select v1.
Now all the adjacent vertices to v1 are identified and all of thoseare visited.
Next v2 is selected and all its adjacent vertices visited and so on.
-
8/3/2019 Chapter Seven Graphs
26/44
5/3/12
Graph Traversal
Depth First Search
Let us start with V1.
V4
V8
V7V6V5
V2 V3
V1
G
-
8/3/2019 Chapter Seven Graphs
27/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent vertices are V2, V8, and V3.
Let us pick on V2.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
28/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent vertices are V1, V4, V5.
V1 is already visited.
Let us pick on V4.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
29/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent vertices are V2, V8.
V2 is already visited.
Let us pick on V8.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
30/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent vertices are V4, V5, V1, V6, V7.
V4 and V1 are already visited.
Let us pick on V5.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
31/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent vertices are V2, V8.
Both are already visited.
Therefore we back track.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
32/44
5/3/12
Graph Traversal
Depth First Search
We have V6 and V7 unvisited in the list of V8.
We may visit any. We visit V6.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
33/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent are V8 and V3.
Obviously the choice is V3.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
34/44
5/3/12
Graph Traversal
Depth First Search
Its adjacent vertices are V1, V7.
We visit V7.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
35/44
5/3/12
Graph Traversal
Depth First Search
We may implement the depth first search method by using astack,
pushing all unvisited vertices adjacent to the one just visited and
popping the stack to find the next vertex to visit.
Algorithm
Iterative version:
During the execution of these algorithms, each node will be inone of the following states, called the status of node n.
status=1(Ready state) :- Initial state of the node 'n'.
status=2(waiting state):- 'n' is on queue or stack.
G h T l
-
8/3/2019 Chapter Seven Graphs
36/44
5/3/12
Graph Traversal
Depth First Search
Step1: Initialize all nodes to the ready state i.e. status=1.
Step 2: Push the starting node A onto stack and change itsstatus=2.
Step 3: Repeat while(stack !=empty)a) Pop the top node 'n' of the stack. Process 'n' and change
its
status to the processed state(i.e status=3)
b) Push onto stack all the neighbours of 'n' that are still inthe
ready state and change their status to the waitingstate(i.e.
=
G h T l
-
8/3/2019 Chapter Seven Graphs
37/44
5/3/12
Graph Traversal Breadth First Search
In DFS we pick on one of the adjacent vertices; visit all of theadjacent vertices and back track to visit the unvisited adjacentvertices.
In BFS we first visit all the adjacent vertices of the start vertex
and then visit all the unvisited vertices adjacent to these and soon.
V4
V8
V7V6V5
V2 V3
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
38/44
5/3/12
Graph Traversal Breadth First Search
We start with V1. Its adjacent vertices are V2, V8, V3. We visitall one by one.
We pick on one of these, say V2. The unvisited adjacentvertices to V2 are V4, V5. We visit both.
V1
V2 V3
V8
V1
V5V4
V3V8V2
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
39/44
5/3/12
Graph Traversal Breadth First Search
We go back to the remaining unvisited adjacent vertices of V1and pick on one of those say V3.
The unvisited adjacent vertices are V6, V7.
There are no more unvisited adjacent vertices of V8, V4, V5,V6 and V7.
Thus the sequence so generated is
V7V6V5V4
V3V8V2
V1
G h T l
-
8/3/2019 Chapter Seven Graphs
40/44
5/3/12
Graph Traversal Breadth First Search
Here we need a queue instead of a stack to implement it.
We add unvisited vertices adjacent to the one just visited, at the rearand
read at front to find the next vertex to visit.
Abstract view of BFS Algorithm:
Step1: Initialize all nodes to the ready state i.e. status=1.
Step 2: Put the starting node A onto queue and change its
status=2.
Step 3: Repeat while(queue !=empty)
a) Remove the front node 'n' of the queue. Process 'n' andchange
G h T l
-
8/3/2019 Chapter Seven Graphs
41/44
5/3/12
Graph Traversal Breadth First Search
Detailed view of BFS algorithm:
bfs (vertex v)
{
vertex w;
queue q;
visited [v] = true;
initialise (q);
addqueue (q,v)
while (! Emptyqueue(q))
{
deletequeue (q,v);
for all vertices w adjacent to v
if (!visited [w])
-
8/3/2019 Chapter Seven Graphs
42/44
5/3/12
Shortest Path Problem
-
8/3/2019 Chapter Seven Graphs
43/44
5/3/12
Minimum Spanning tree
-
8/3/2019 Chapter Seven Graphs
44/44
Thank you!!!
top related