data structure and algorithms

Post on 22-Jan-2016

22 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Data Structure and Algorithms. Lecture 11 Graphs. What is a Graph?. A graph consists of a number of data items, each of which is called a vertex. Any vertex may be connected to any other, these connections are called edges. A graph G = ( V ,E) is composed of: V : set of vertices - PowerPoint PPT Presentation

TRANSCRIPT

Computer Science Department

Data Structure and Algorithms

Lecture 11

Graphs

Computer Science Department

What is a Graph?• A graph consists of a number of data items, each of which is

called a vertex. Any vertex may be connected to any other, these connections are called edges.

• A graph G = (V,E) is composed of:

V: set of vertices

E: set of edges connecting the vertices in V• An edge e = (u,v) is a pair of vertices• Example:

a b

c

d e

V= {a,b,c,d,e}

E= {(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),(d,e)}

Computer Science Department

Applications

• Electronic circuits– Printed circuit board– Integrated circuit

• Transportation networks– Highway network– Flight network

• Computer networks– Local area network– Wide area network– Internet

• Databases– Entity-relationship diagram

Computer Science Department

Directed and Undirected Graph

• Undirected graph– When the edges in a graph have no direction, the graph is

called undirected

Computer Science Department

Directed and Undirected Graph

• When the edges in a graph have a direction, the graph is called directed graph .

• Also known as digraph.• This kind of graph contains ordered pair of vertices i.e. if the

graph is directed, the order of the vertices in each edge is important.

A

B

C D

E

V = {A, B, C, D, E}E = {(A,B), (B,C), (C,B), (A,C), (A,E), (C,D)}

Computer Science Department

Graph Terminologies

• Weighted Graph– A graph is suppose to be weighted if its every edge is

assigned some value which is greater than or equal to zero.

Computer Science Department

Graph Terminologies

• Adjacent Nodes– When there is an edge from one node to another then these

nodes are called adjacent nodes.

• Path– sequence of vertices v1,v2,. . .vk such that consecutive

vertices vi and vi+1 are adjacent.

a b

c

d e

a b

c

d e

a b e d c b e d c

Computer Science Department

Graph Terminologies

• Length of a Path– Length of a path is nothing but the total number of edges

included in the path from source to destination node.

• Simple Path– path such that all its vertices and edges are distinct.

a b

c

d e

b e c

Computer Science Department

Graph Terminologies

• Cycle– simple path, except that the last vertex is the same as the

first vertex

• Acyclic Graph– A graph without cycle is

called acyclic Graph. A treeis a good example of acyclicgraph.

a b

c

d ea b e c a

Computer Science Department

Graph Terminologies

• Connected Graph– An undirected graph is connected if, for any pair of vertices,

there is a path between them.

– A directed graph is connected if, for any pair of vertices, there is a path between them.

Connected Not Connected

Computer Science Department

Graph Terminologies• Complete Graph

– A graph in which all pairs of vertices are adjacent OR– A graph in which every vertex is directly connected to every

other vertex– Let n = Number of vertices, and

m = Number of edges– For a complete graph with n vertices, the number of edges

is n(n – 1)/2. A graph with 6 vertices needs 15 edges to be complete.

n 5m (5

Computer Science Department

Graph Terminologies

• Degree of a node– In an Undirected graph, the total number of edges linked to

a node is called a degree of that node.– For directed graph there are two degree for every node

• Indegree– The indegree of a node is the total number of edges

coming to that node.

• Outdegree– The outdegree of a node is the total number of edges

going out from that node

Computer Science Department

Graph Terminologies0

1 2

3 4 5 6G

G2

3

2

3 3

1 1 1 1

Directed graph

in-degreeout-degree

0

1

2

G3

in:1, out: 1

in: 1, out: 2

in: 1, out: 0

0

1 2

3

33

3

Computer Science Department

Graph Representation

• There are two methods to represent a Graph– Adjacency Metrix ( Array Implementation)– Adjacency List (Linked List Implementation)

Computer Science Department

Adjacency Matrix

• Let G=(V,E) be a graph with n vertices.• The adjacency matrix of G is a two-dimensional

n by n array, say adj_mat• If the edge (vi, vj) is in E(G), adj_mat[i][j]=1• If there is no such edge in E(G), adj_mat[i][j]=0• The adjacency matrix for an undirected graph is

symmetric; since adj_mat[i][j]=adj_mat[j][i] • The adjacency matrix for a digraph may not be

symmetric

Computer Science Department

Examples for Adjacency Matrix

0

1

1

1

1

0

1

1

1

1

0

1

1

1

1

0

0

1

0

1

0

0

0

1

0

0

1

1

0

0

0

0

0

1

0

0

1

0

0

0

0

1

0

0

1

0

0

0

0

0

1

1

0

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

1

0

1

0

0

0

0

0

0

1

0

1

0

0

0

0

0

0

1

0

G1

G2

G4

0

1 2

3

0

1

2

1

0

2

3

4

5

6

7

symmetric

Computer Science Department

Adjacency Matrix

• From the adjacency matrix, to determine the connection of vertices is easy

• The degree of a vertex is

• We can also represent weighted graph with Adjacency Matrix. Now the contents of the martix will not be 0 and 1 but the value is substituted with the corresponding weight.

adj mat i jj

n

_ [ ][ ]

0

1

Computer Science Department

Another Example Adjacency Matrix?

1 2

53

76

4

Computer Science Department

1234567

10111000

20001100

30000010

40010011

50001001

60000000

70000010

Computer Science Department

Adjacency List

• A Single Dimension array of Structure is used to represent the vertices

• A Linked list is used for each vertex V which contains the vertices which are adjacent from V (adjacency list)

Computer Science Department

0123

012

01234567

1 2 30 2 30 1 30 1 2

G1

10 2

G3

1 20 30 31 254 65 76G4

0

1 2

3

0

1

2

1

0

2

3

4

5

6

7

Computer Science Department

Another Example of Adjacency List

Computer Science Department

Linked-list implementation

Computer Science Department

Graph Traversal• Traversal is the facility to move through a structure

visiting each of the vertices once.

• We looked previously at the ways in which a binary tree can be traversed. Two of the traversal methods for a graph are breadth-first and depth-first.

Computer Science Department

Breadth-First Graph Traversal• This method visits all the vertices, beginning with a specified

start vertex. It can be described roughly as “neighbours-first”. • No vertex is visited more than once, and vertices are visited

only if they can be reached – that is, if there is a path from the start vertex.

• Breadth-first traversal makes use of a queue data structure. The queue holds a list of vertices which have not been visited yet but which should be visited soon.

• Since a queue is a first-in first-out structure, vertices are visited in the order in which they are added to the queue.

• Visiting a vertex involves, for example, outputting the data stored in that vertex, and also adding its neighbours to the queue.

• Neighbours are not added to the queue if they are already in the queue, or have already been visited.

Computer Science Department

Breadth-First Traversal Example• Neighbours are added to the queue in alphabetical order.

Visited and queued vertices are shown as follows:

Computer Science Department

Breadth-First Graph Traversal

• Note that we only add Denver to the queue as the other neighbours of Billings are already in the queue.

Computer Science Department

Breadth-First Graph Traversal

• Note that nothing is added to the queue as Denver, the only neighbour of Corvallis, is already in the queue.

Computer Science Department

Breadth-First Graph Traversal

Computer Science Department

Breadth-First Graph Traversal

Computer Science Department

Breadth-First Graph Traversal

Computer Science Department

Breadth-First Graph Traversal

• Note that Grand Rapids was not added to the queue as there is no path from Houston because of the edge direction. • Since the queue is empty, we must stop, so the traversal is complete.• The order of traversal was:Anchorage, Billings, Corvallis, Edmonton, Denver, Flagstaff, Houston

Computer Science Department

Exercise Solve it using BFS

A

C D B

E

F G

I H

Computer Science Department

Exercise 2Solve it using BFS

A

C D

B

E

F G

H

Computer Science Department

Depth-First Search (DFS)

Computer Science Department

Depth-First Search

1. From the given vertex, visit one of its adjacent vertices and leave others;

2. Then visit one of the adjacent vertices of the previous vertex;

3. Continue the process, visit the graph as deep as possible until:

– A visited vertex is reached;– An end vertex is reached.

Computer Science Department

Depth-First Traversal

1. Depth-first traversal of a graph:

2. Start the traversal from an arbitrary vertex;

3. Apply depth-first search;

4. When the search terminates, backtrack to the previous vertex of the finishing point,

5. Repeat depth-first search on other adjacent vertices, then backtrack to one level up.

6. Continue the process until all the vertices that are reachable from the starting vertex are visited.

7. Repeat above processes until all vertices are visited.

Computer Science Department

Depth First Search Traversal• This method visits all the vertices, beginning with a specified

start vertex. • This strategy proceeds along a path from vertex V as deeply

into the graph as possible.• This means that after visiting V, the algorithm tries to visit any

unvisited vertex adjacent to V. • When the traversal reaches a vertex which has no adjacent

vertex, it back tracks and visits an unvisited adjacent vertex.

• Depth-first traversal makes use of a Stack data structure.

Computer Science Department

DFS Example

A

D E

B

F

C

A B C F E D

Computer Science Department

Adjacency List Implementation

class Graph

{

private:

struct GNode{

int data;

GNode *ptr;

};

GNode *GList;

public:

graph();

void Create();

void InsertVertex(int);

void InsertEdge();

void DisplayVertices();

void DeleteVertex();

void DeleteEdges();

bool SearchVertex();

bool SearchEdge();

void BFSTraversal();

void DFSTraversal();

};

Computer Science Department

create) (void Graph::Create()

{

GList = new GNode[10];

for(int i=0; i<10; i++)

{

GList[i].data=-1;

GList[i].ptr=NULL;

}

}

Computer Science Department

InsertVertex) (void Graph::InsertVertex ( int VCount )

{

int num;

cout<<"Enter Vertex/Node Number =";

cin>>num;

GList[VCount].data=num;

}

Computer Science Department

InsertEdge) (void Graph::InsertEdge()

{ int i, source,dest;

cout<<"Enter Source Node=";

cin>>source;

cout<<"Enter Destinition Node=";

cin>>dest;

for(i=0;i<10;i++)

if(source==GList[i].data)

break;

GNode *check,*temp =new GNode;

temp->data=dest;

temp->ptr=NULL;

check = GList[i].ptr;

Computer Science Department

if(check!=NULL)

{

while(check->ptr!=NULL)

{

check=check->ptr;

}

check->ptr=temp;

}

else

GList[i].ptr=temp;

getche();

}

Computer Science Department

SearchVertex) (bool Graph::SearchVertex()

{

int vertex;

cout<<"Enter Vertex To Be Searched=";

cin>>vertex;

for(int i=0;i<10;i++)

if(GList[i].data ==vertex)

return true;

return false;

}

Computer Science Department

SearchEdge) (bool Graph::SearchEdge()

{

int source, dest;

GNode *temp;

cout<<"Enter Edge to be searched"<<endl;

cout<<"Enter Source Vertex=";

cin>>source;

cout<<"Enter Destination Vertex=";

cin>>dest;

Computer Science Department

for( int i=0;i<10;i++)

{

if(source==GList[i].data)

{

temp=GList[i].ptr;

while(temp!=NULL)

{

if(temp->data==dest)

return true;

else

temp=temp->ptr;

}

}

}

return false;

}

top related