0% found this document useful (0 votes)
3 views136 pages

04 Unit DS Notes

A graph is a data structure consisting of nodes (vertices) and edges that describe relationships among the nodes. Graphs can be directed or undirected, with specific terminology such as adjacent nodes, paths, and degrees of vertices. Various representations of graphs include adjacency matrices and lists, and common operations include traversal methods like Depth First Search (DFS) and Breadth First Search (BFS).

Uploaded by

bhavanisain2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views136 pages

04 Unit DS Notes

A graph is a data structure consisting of nodes (vertices) and edges that describe relationships among the nodes. Graphs can be directed or undirected, with specific terminology such as adjacent nodes, paths, and degrees of vertices. Various representations of graphs include adjacency matrices and lists, and common operations include traversal methods like Depth First Search (DFS) and Breadth First Search (BFS).

Uploaded by

bhavanisain2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 136

Graphs

What is a graph?
 Is a data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes
to each other
 The set of edges describes relationships among
the vertices
Formal definition of graphs
 A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of
vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs

 When the edges in a graph have no


direction, the graph is called undirected
Directed vs. undirected graphs
(cont.)

 When the edges in a graph have a direction,


the graph is called directed (or digraph)

NOTE: if the graph is directed, the order of the


vertices in each edge is important !!
Examples for Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph G3

V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={(0,1),(1,0),(1,2)}

complete undirected graph: n(n-1)/2 edges


complete directed graph: n(n-1) edges
CHAPTER 6 6
Trees vs graphs

 Trees are special cases of graphs!!


Graph terminology
 Adjacent nodes: two nodes are adjacent if
they are connected by an edge
5 is adjacent to 7
7 is adjacent from 5

 Path: a sequence of vertices that connect


two nodes in a graph
 Complete graph: a graph in which every
vertex is directly connected to every other
vertex
Complete Graph
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the
maximum number of edges is n(n-1)/2
– for directed graph with n vertices, the
maximum
number of edges is n(n-1)
– example: G1 is a complete graph

CHAPTER 6 9
Graph terminology (cont.)
 What is the number of edges in a
complete directed graph with N vertices?
N * (N-1)

2
O( N )
Graph terminology (cont.)
 What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
O( N )
Graph terminology (cont.)
 Weighted graph: a graph in which each edge
carries a value
Degree
 The degree of a vertex is the number of
edges incident to that vertex
 For directed graph,
– the in-degree of a vertex v is the number of
edges
that have v as the head
– the out-degree of a vertex v is the number of
edges
that have v as the tail
The degree of a vertex in a graph is
the number of edges incident on that
vertex.
CHAPTER 6 13
undirected graph
degree
3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3
G13 1 1 G2 1 1
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2

2 in: 1, out: 0
G3
CHAPTER 6 14
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where
each
edge is a pair of vertices
functions: for all graph  Graph, v, v1 and v2  Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v

CHAPTER 6 15
Graph Representations
 Adjacency Matrix
 Adjacency Lists
 Adjacency Multilists

CHAPTER 6 16
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; the adjacency matrix for a digraph
need not be symmetric

CHAPTER 6 17
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1    7
  1 0 1 
2 0 1 1 0 0 0 0 0
1 1 0 0 0 1 0
1 0
   0 0 1 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2  
G
1 0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
 
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
 
undirected: n2/2 0 0 0 0 0 0 1 0
directed: n2
CHAPTER 6 G4 18
Data Structures for Adjacency
Lists
Each row in adjacency matrix is represented as an adjacenc

#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use *

CHAPTER 6 19
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
An undirected graph with n vertices and e edges ==> n headCHAPTER
nodes6 and 2e list20no
Figure 6.10: Inverse adjacency list for G3

0
0  1 NULL

1 1  0 NULL

2  1 NULL
2

Determine in-degree of a vertex in a fast way.

CHAPTER 6 21
Figure 6.11: Alternate node structure for adjacency lists (p.267)

tail head column link for head row link for tail

CHAPTER 6 22
Figure 6.12: Orthogonal representation for graph G3(p.268)

0 1 2

0 0 1 NULL NULL

1 1 0 NULL 1 2 NULL NULL

0
2 NULL
 0 1 0
 
 1 0 1 
0 0 0
1

2
CHAPTER 6 23
Figure 6.13:Alternate order adjacency list for G1 (p.268)
Order is of no significance.
headnodes vertax link

0  3  1  2 NULL

1  2  0  3 NULL

2  3  0  1 NULL

3  2  1  0 NULL

1 2
3 CHAPTER 6 24
Adjacency Multilists
An edge in an undirected graph is
represented by two nodes in adjacency
list representation.
Adjacency Multilists
–lists in which nodes may be shared among
several lists.
(an edge is shared by two different paths)

marked vertex1 vertex2 path1 path2

CHAPTER 6 25
Example for Adjacency Multlists
Lists: vertex 0: M1->M2->M3, vertex 1: M1->M4->M5
vertex 2: M2->M4->M6, vertex 3: M3->M5->M6
(1,0)
0 N 0 1 N2 N4 edge (0,1)
1 (2,0)
2 1 0 2 N3 N4 edge (0,2)
(3,0)
3 N 0 3 N5 edge (0,3)
(2,1)

0 2 1 2 N5 N6 edge (1,2)
(3,1)
N 1 3 N6 edge (1,3)
1 2 (3,2)
3 2 3 edge (2,3)
3
N six edges
CHAPTER 6 26
Adjacency Multilists

typedef struct edge *edge_pointer;


typedef struct edge {
short int marked;
int vertex1, vertex2;
edge_pointer path1, path2;
};
edge_pointer graph[MAX_VERTICES];

marked vertex1 vertex2 path1 path2

CHAPTER 6 27
Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal

CHAPTER 6 28
BFS: Level-by-level traversal
 Given a starting vertex s
 Visit all vertices at increasing distance from s
 Visit all vertices at distance k from s
 Then visit all vertices at distance k+1 from s
 Then ….

1
The general BFS algorithm
 Each vertex can be in one of three states:
 Unmarked and not on queue
 Marked and on queue
 Marked and off queue
 The algorithm moves vertices between these
states

2
Handling vertices
 Unmarked and not on queue:
 Not reached yet
 Marked and on queue:
 Known, but adjacent vertices not visited yet (possibly)
 Marked and off queue:
 Known, all adjacent vertices on queue or done with

3
Queue: A Resultant Array:
A

B E

G C D

Start with A. Mark it.

4
Queue: A B E Resultant Array: A
A

B E

G C D

Expand A’s adjacent vertices.


Mark them and put them in queue.
5
Queue: A B E C G Resultant Array: A B
A

B E

G C D

Now take B off queue, and queue its neighbors.

6
Queue: A B E C G D F Resultant Array:
ABE
A

B E

G C D

Do same with E.

7
Queue: A B E C G D F Resultant Array:
ABEC

B E

G C D

Visit C.
Its neighbor F is already marked, so not queued.
8
Queue: A B E C G D F Resultant Array:
ABECG

B E

G C D

Visit G.

9
Queue: A B E C G D F Resultant Array:
ABECGD

B E

G C D

Visit D. F, E marked so not queued.

10
Queue: A B E C G D F Resultant Array:
ABE CGDF

B E

G C D

Visit F.
E, D, C marked, so not queued again.
11
Queue: A B E C G D F
A

B E

G C D

Done. We have explored the graph in order:


A B E C G D F.
12
Interesting features of BFS
 Complexity: O(|V| + |E|)
 All vertices put on queue exactly once
 For each vertex on queue, we expand its edges
 In other words, we traverse all edges once
 BFS finds shortest path from s to each vertex
 Shortest in terms of number of edges

13
Depth-first search

 Again, a simple and powerful algorithm


 Given a starting vertex s
 Pick an adjacent vertex, visit it.
 Then visit one of its adjacent vertices
 …..
 Until impossible, then backtrack, visit another

1
DFS(graph g, vertex s)
Assume all vertices initially unmarked

1. mark s
2. visit s // e.g., print its data
3. for each edge <s, V>
4. if V is not marked
5. DFS(G, V)

2
Current vertex: A
A

B E

G C D

Start with A. Mark it.

3
Current: B
A

B E

G C D

Expand A’s adjacent vertices. Pick one (B).


Mark it and re-visit.
4
Current: C
A

B E

G C D

Now expand B, and visit its neighbor, C.

5
Current: F
A

B E

G C D

Now expand C, and visit its neighbor, F


Visit F.
6
Current: E
A

B E

G C D

Pick one of its neighbors, E.


7
Current: D
A

B E

G C D

E’s adjacent vertices are A, D and F.


A and F are marked, so pick D.
Visit D. 8
Current: G
No new vertices available. Backtrack to
E. Backtrack to F. Backtrack to C. Backtrack to B.

B E

G C D

Adjacent B is A and G. F

Visit G.
No new vertices from here. Backtrack to A. E
already marked so no new vertices.
Current:
1
A
2
5
B E
3 6
G 7 C D

4
F

Done. We have explored the graph in order:


ABCFEDG
10
Interesting features of DFS
 Complexity: O(|V| + |E|)
 All vertices visited once, then marked
 For each vertex on stack , we examine all edges
 In other words, we traverse all edges once

11
//Implementation of Bubble Sort
#include<stdio.h>
#include<conio.h>
#define max 100
void main()
{
int x[max],i,j,temp,n;
clrscr();
printf("\n Enter the size of an array :");
scanf("%d",&n);
printf("\n Enter the Elements in to an array:");
for(i=0;i<n;i++)
scanf("%d",&x[i]);

for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(x[j] > x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
} // end for innerloop
} // end for outer loop

printf("\n Sorted Elements are:\n");

for(i=0;i<n;i++)
printf("%d ",x[i]);
getch();
}
//Implementation of Selection Sort

#include<stdio.h>
#include<conio.h>
#define max 100
void main()
{
int x[max],i,j,temp,n,min;
clrscr();
printf("\n Enter the size of an array :");
scanf("%d",&n);
printf("\n Enter the Elements in to an array:\n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);

for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(x[min] > x[j])
min=j;
}
temp=x[i];
x[i]=x[min];
x[min]=temp;
} // end for outer loop

printf("\n Sorted Elements are:\n");


for(i=0;i<n;i++)
printf("%d ",x[i]);
getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy