CHAPTER 6

1 / 19

# CHAPTER 6 - PowerPoint PPT Presentation

CHAPTER 6. GRAPH. CSEB324 DATA STRUCTURES &amp; 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CHAPTER 6' - rigg

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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
• vertices. G={V,E};
• Nodes are the data element of the graph and edge is
• the connector between two nodes.
• E.g.

A

B

C

G

D

E

F

Terminologies

• 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)

A

B

C

G

D

E

F

Terminologies

Cycle:

• It is a simple path except that the first and last vertices are the same. (ABDFA)

B

B

A

A

C

C

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

B

A

C

5

4

7

1

2

1 2 3 4

1 0 11 0

2 0 0 11

3 0 0 0 1

4 11 0 0

4

3

Graph Representation

• 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.
Example for directed graph and its adjacency matrix

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

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

1

2

4

3

Graph Representation

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

1. List of vertices

2. For each vertex, a list of adjacent vertices

Vertex Set

1 {2,3}

2 {3,4}

3 4

4 {1,2}

Example for directed graph and its adjacency list

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

Example for undirected graph and its adjacency list

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

More example for Graphs

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).

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)

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. Designatethis as search node and mark it as visited.

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.

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.