Ankur Graph
Ankur Graph
Name :
Register No. :
Day / Session :
Date of conduction:
Date of Submission:
Marks
Particulars Max. Marks
Obtained
Pre lab 10
Lab Performance 15
Post lab 10
Viva 05
Total 40
REPORT
VERIFICATION
Staff Name :
Signature
Experiment No – 7
GRAPHS
Objectives:
To explore what a graph is and how it is used.
To implement the graph abstract data type using multiple internal representations.
To see how graphs can be used to solve a wide variety of problems
Check if the element is present in the graph
Graph Traversal
Add elements(vertex, edges) to graph
Finding the path from one vertex to another
Graph
Graph is defined as an ordered set <V, E>, where V(G) represents the set of vertices in the
graph G and E(G) represents the set of Edges in the graph G. Before proceeding further, let's
discuss some frequent terms used in graph concept –
Vertex − Each node of the graph is represented as a vertex. In
example given below, labelled circle represents vertices. So A to G
are vertices. We can represent them using an array as shown in
image below. Here A can be identified by index 0. B can be
identified using index 1 and so on.
Edge − Edge represents a path between two vertices or a line
between two vertices. In example given below, lines from A to B, B
to C and so on represents edges. We can use a two dimensional
array to represent array as shown in image below. Here AB can be
represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and
so on, keeping other combinations as 0.
Adjacency − Two node or vertices are adjacent if they are
connected to each other through an edge. In example given below,
B is adjacent to A, C is adjacent to B and so on.
Path − Path represents a sequence of edges between two vertices.
In example given below, ABCD represents a path from A to D.
Fig 1: Graph
A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed as vertices,
and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of
edges, connecting the pairs of vertices. Take a look at the following graph –
Fig.2: Graph
In the above graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
Basic Operations
Following are basic primary operations of a Graph which are following.
Add Vertex − add a vertex to a graph.
Add Edge − add an edge between two vertices of a graph.
Display Vertex − display a vertex of a graph.
Declaring a Graph
Graph Traversal
1. Depth First Traversal
Depth First Search algorithm (DFS) traverses a graph in a depth ward motion and uses a stack
to remember to get the next vertex to start a search when a dead end occurs in any iteration.
Fig.3: Graph for DFS
As in example given above, DFS algorithm traverses from A to B to C to D first then to E,
then to F and lastly to G. It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark, it visited. Display it.
Push it in a stack.
Rule 2 − If no adjacent vertex found, pop up a vertex from stack. (It
will pop up all the vertices from the stack which do not have
adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
2. Breadth First Traversal
Breadth First Search algorithm (BFS) traverses a graph in a breadth wards motion and uses a
queue to remember to get the next vertex to start a search when a dead end occurs in any
iteration.
As in example given below, BFS algorithm traverses from A to B to E to F first then to C and
G lastly to D. It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it.
Insert it in a queue.
Rule 2 − If no adjacent vertex found, remove the first vertex from
queue.
Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
V1, V2, V3, V4 and V1, V3, V2, V4 are legal orderings
Outdegree of a vertex U: the number of edges (U,V) - outgoing edges
Indegree of a vertex U: the number of edges (V,U) - incoming edges
Degree of a vertex U: total number of edges associated with u.
The algorithm for topological sort uses "indegrees" of vertices.
Algorithm
1. Compute the indegrees of all vertices
2. Find a vertex U with indegree 0 and print it (store it in the ordering)
If there is no such vertex then there is a cycle and the vertices cannot be ordered. Stop.
3. Remove U and all its edges (U,V) from the graph.
4. Update the indegrees of the remaining vertices.
5. Repeat steps 2 through 4 while there are vertices to be processed.
Algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If
cycle
is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Prims Algorithm
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses
greedy approach. Prim's algorithm shares similarity with shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and
keeps on adding new nodes to the spanning tree from the given graph.
Step 1 - Remove all loops & Parallel Edges
Step 2 - Choose any arbitrary node as root node
Step 3 - Check outgoing edges and select the one with less cost
Pseudocode for Prim’s Algorithm
Algorithm
1) Create a set mstSet that keeps track of vertices already included in MST.
2) Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE.
Assign key value as 0 for the first vertex so that it is picked first.
3) While mstSet doesn’t include all vertices
a) Pick a vertex u which is not there in mstSet and has minimum key value.
b) Include u to mstSet.
c) Update key value of all adjacent vertices of u. To update the key values, iterate
through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is
less than the previous key value of v, update the key value as weight of u-v.
Pre-Lab:
1. What are the different operations can be performed in graphs?
2. What are the different ways graphs are represented?
Post-Lab:
1. Write a logic to find whether there is a path between two nodes or not?
2. Write a logic to find whether a graph is tree or not?
Hard Level:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_N 15
#define nmax MAX_N
visited[node] = 0;
}
int main() {
int t;
scanf("%d", &t);
while(t--){
int M;
scanf("%d %d", &N, &M);
memset(graph, 0, sizeof(graph));
memset(max_path, 0, sizeof(max_path));
return 0;
}
Output:
2.
Code:
#include <stdio.h>
#include <stdbool.h>
#define MAX 20
int n, m;
char grid[MAX][MAX];
bool used[MAX][MAX];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int solve() {
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] != 'a' || used[i][j]) continue;
for (int d1 = 0; d1 < 4; d1++) {
int x1 = i + dx[d1], y1 = j + dy[d1];
if (!is_valid(x1, y1) || grid[x1][y1] != 'b') continue;
for (int d2 = 0; d2 < 4; d2++) {
int x2 = x1 + dx[d2], y2 = y1 + dy[d2];
if (!is_valid(x2, y2) || grid[x2][y2] != 'c') continue;
for (int d3 = 0; d3 < 4; d3++) {
int x3 = x2 + dx[d3], y3 = y2 + dy[d3];
if (is_valid(x3, y3) && grid[x3][y3] == 'd') {
count++;
used[i][j] = used[x1][y1] = used[x2][y2] = used[x3][y3]
= true;
goto next_iteration;
}
}
}
}
next_iteration:;
}
}
return count;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
scanf(" %c", &grid[i][j]);
used[i][j] = false;
}
printf("%d\n", solve());
return 0;
}
Output:
RESULT:
IN THIS PROGRAM 5 EASY , 3 MEDIUM AND 2 HARD LEVEL PROBLEMS
WERE SOLVED AND RESULTS WERE VERIFIED.