chapter 6 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CHAPTER 6 PowerPoint Presentation
Download Presentation
CHAPTER 6

Loading in 2 Seconds...

play fullscreen
1 / 19

CHAPTER 6 - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

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

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

PowerPoint Slideshow about 'CHAPTER 6' - rigg


An Image/Link below is provided (as is) to download presentation

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

CHAPTER 6

GRAPH

CSEB324

DATA STRUCTURES & ALGORITHM

introduction

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

A

B

C

G

D

E

F

Terminologies

Adjacent:

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

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

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

graph representation

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

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

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

example for weighted undirected graph and its adjacency matrix
Example for weighted undirected graph and its adjacency matrix

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

graph representation1

1

2

4

3

Graph Representation

2. Adjacency List

  • 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
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
Example for undirected graph and its adjacency list

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

more example for graphs
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
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)

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

breadth first search bfs
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.

breadth first search bfs1
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.