+ All Categories
Home > Documents > CHAPTER 6

CHAPTER 6

Date post: 21-Jan-2016
Category:
Upload: rigg
View: 28 times
Download: 0 times
Share this document with a friend
Description:
CHAPTER 6. GRAPH. CSEB324 DATA STRUCTURES & ALGORITHM. A. Nodes/vertices: A, B, C, G, D, E, F Edges : AB, BC, CG, BD, CE, AF and DF. B. C. G. D. E. F. Introduction. Graph : A graph G consists of two: a set V of vertices or nodes and a set E of edges/arcs that connect the - PowerPoint PPT Presentation
Popular Tags:
19
CHAPTER 6 CHAPTER 6 GRAPH CSEB324 CSEB324 DATA STRUCTURES & ALGORITHM DATA STRUCTURES & ALGORITHM
Transcript
Page 1: CHAPTER 6

CHAPTER 6CHAPTER 6GRAPH

CSEB324CSEB324DATA STRUCTURES & ALGORITHMDATA STRUCTURES & ALGORITHM

Page 2: CHAPTER 6

INTRODUCTIONGraph : A graph G consists of two: a set V of vertices or nodes and a set E of edges/arcs that connect the vertices. G={V,E}; Nodes are the data element of the graph and

edge is the connector between two nodes. E.g.

2

Nodes/vertices:A, B, C, G, D, E, FEdges:AB, BC, CG, BD, CE,AF and DF

A

F

BD

C

EG

Page 3: CHAPTER 6

TERMINOLOGIESAdjacent: Two vertices of a graph are adjacent it they are

joined by an edge.Subgraph: A subgraph consists of a subset of a graph’s vertices

and a subset of its edges. Path: A path between two vertices is a sequence of edges

that begins at one vertex and ends at another vertex. (ABDFA is a path)

Simple path: It is a path in which no vertex is repeated. (ABDF is a simple path)

3

A

F

BD

CE

G

Page 4: CHAPTER 6

TERMINOLOGIES

Cycle: It is a simple path except that the first and

last vertices are the same. (ABDFA)

4

A

F

BD

CE

G

Page 5: CHAPTER 6

TERMINOLOGIES

Directed graph or Digraph: A graph whose edges have direction.

Undirected Graph A graph whose edges have no direction.

Weighted graph It is a graph whose edges have costs associated with them Can be directed or undirected

5

A B

C

A B

C

A B

C5

74

Page 6: CHAPTER 6

GRAPH REPRESENTATION

1. Adjacency Matrix: A graph containing n nodes can be

represented by a matrix containing n rows and n columns.

The matrix is formed by placing a “1” in the ith row and jth column of the matrix if there is an edge between node i and node j of the graph.

6

1 2

34

1 2 3 41 0 1 1 02 0 0 1 13 0 0 0 14 1 1 0 0

Page 7: CHAPTER 6

EXAMPLE FOR DIRECTED GRAPH AND ITS ADJACENCY MATRIX

7

(a) A directed graph and (b) its adjacency matrix

Page 8: CHAPTER 6

EXAMPLE FOR WEIGHTED UNDIRECTED GRAPH AND ITS ADJACENCY MATRIX

8

(a) A weighted un directed graph and (b) its adjacency matrix

Page 9: CHAPTER 6

GRAPH REPRESENTATION

2. Adjacency List A graph can be represented as a List. A list has the following :

1. List of vertices2. For each vertex, a list of adjacent vertices

9

1 2

34

VertexSet1 {2,3}2 {3,4}3 44 {1,2}

Page 10: CHAPTER 6

EXAMPLE FOR DIRECTED GRAPH AND ITS ADJACENCY LIST

(a) A directed graph and (b) its adjacency list

10

Page 11: CHAPTER 6

EXAMPLE FOR UNDIRECTED GRAPH AND ITS ADJACENCY LIST

11(a) A weighted undirected graph and (b) its adjacency list

Page 12: CHAPTER 6

MORE EXAMPLE FOR GRAPHS

12

NOTE: it is common to include cross links between corresponding edges, when needed to mark the edges previously visited. e.g. (v,w) = (w,v).

Page 13: CHAPTER 6

GRAPH TRAVERSAL What is Traversal?

Visiting Vertices in a Graph is called as traversal.

Purpose of Visiting?For some Processing( Printing and so)

How Many ways of Visiting?1. Depth First Search (DFS)2. Breadth First Search (BFS) 13

Page 14: CHAPTER 6

DEPTH FIRST SEARCH (DFS) The main logic of the depth-first-search is

analogous to the preorder traversal of a tree. It is accomplished recursively as follows:

Step1: Choose any node in the graph. Designate it as search node and mark it as visited.

Step2: Using adjacency matrix of the graph, find a node adjacent to the search node (connected by an edge from the search node) that has not been visited yet. Designate this as search node and mark it as visited.

14

Page 15: CHAPTER 6

DEPTH FIRST SEARCH (DFS)Step3: Repeat step 2 using the new search

node. If no nodes satisfying step 2 can be found, return to the previous search node and continue from there

Step4: When a return to the previous search node in step 3 is impossible, the search from the originally chosen search node is complete.

Step5: if the graph still contains unvisited nodes, choose any node that has not been visited and repeat steps 1 through 4. 15

Page 16: CHAPTER 6

DEPTH FIRST SEARCH (DFS)

16

Page 17: CHAPTER 6

BREADTH FIRST SEARCH (BFS)Step1: Begin with any node in the

graph and mark it as visited.

Step2: Proceed to the next node having an edge connection to the node in step 1. Mark it as Visited.

Step3: Come back to the node in step 1, descend along an edge towards an unvisited node, and mark the new node as visited. 17

Page 18: CHAPTER 6

BREADTH FIRST SEARCH (BFS)

Step4: Repeat step 3 until all nodes adjacent to the node in step 1 have been marked as visited.

Step5: Repeat step 1 through 4 starting from the node visited in step 2, then starting from the node visited in step 3 in the order visited.

18

Page 19: CHAPTER 6

THE END

19


Recommended