Docsity

Data Structure - Graphs- Notes, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structure - Graphs- Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Graphs

  • (^) Directed Graph(Digraph)G:

G is a pair (V, E), where V is a finite set and E

is a binary relation on V.

 The set V is called the vertex set of G, its

elements are called vertices.

The set E is called edge set of G, its

elements are called edges.

Undirected Graph

  • (^) G=(V, E) the edge set E consists of

unordered pairs of vertices, rather than

ordered pairs

6

2 3

4 5

1 Directed Graph

V={1,2,3,4,5, 6} E ={(1,2),(2,2),(2,4), (2,5), (4,1),(4,5),(5,4), (6,3)}

  • (^) Degree: degree of a vertex in an undirected graph is the number of edges incident on it.
  • (^) Out degree
  • (^) In degree
  • (^) Path of length k from vertex u to a vertex u’.
  • (^) It is a sequence v 0 , v 1 , v 2 ,…vk of vertices
  • (^) Such that u = v 0 and u’ = vk and (vi-1,,vi) is an edge
  • (^) Length of the path is the number of edges in the path.

Representation

  • (^) Adjacency matrix

• It is a 0-1 matrix A of size |V| * | V|

  • (^) Such that Aij = 1 if (I,j) is an edge of G = 0 other wise

Adjacency List

»It consists of an array say adj

of | V | lists, one for each

vertex. For each vertex u ε V,

adj[u] contains pointer to the

list containing all the vertices

v such that there is an edge

(u,v) ε E.

2 4 ^

253 ^

4 1 2 ^

5 ^

3 4 ^

Adjacency List

5 ^

6 5 ^

2 ^

4 ^

6 ^

Linked Representation of Graphs

  • (^) All the other representations require advance knowledge of the number of nodes in the graph.
  • (^) In this we do not require it.
  • (^) Nodes of the graph are represented by a linked list of header nodes of the form: p Arc ptr info Next node Info (p) contains information associated with that node.
  • (^) Nodeptr(q) is a pointer to the header node

representing the graph node B (the other

end of the arc).

  • (^) Nextarc(q) points to a list node representing

the next arc emanating from the graph

node A

A

C

D

B

E

A ^ B C D ^ E ^

graph

^

<A,B> <A,C> <A,D> <A,E>

^

^ <D,B>

<C,E>