Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views11 pages

Graph Assignement

A graph is a data structure consisting of vertices and edges that model relationships between entities. It can be represented using an adjacency matrix or an adjacency list, each with its own characteristics regarding space efficiency and edge lookup speed. Various types of graphs include directed, undirected, weighted, and complete graphs, and traversal methods like Depth-First Search (DFS) and Breadth-First Search (BFS) are used to visit all vertices systematically.

Uploaded by

vrajpatelmyindia
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)
10 views11 pages

Graph Assignement

A graph is a data structure consisting of vertices and edges that model relationships between entities. It can be represented using an adjacency matrix or an adjacency list, each with its own characteristics regarding space efficiency and edge lookup speed. Various types of graphs include directed, undirected, weighted, and complete graphs, and traversal methods like Depth-First Search (DFS) and Breadth-First Search (BFS) are used to visit all vertices systematically.

Uploaded by

vrajpatelmyindia
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/ 11

5 what is graph ? explain adjacent matrix and adjacent list for graph.

Ans:

a graph is a fundamental data structure used to model relationships between objects. Let’s understand this
step-by-step:

🔷 What is a Graph?

A graph is a collection of:

• Vertices (or nodes): Represent entities.


• Edges (or arcs): Represent connections (relationships) between those entities.

A graph can be:

• Directed: Edges have direction (e.g., one-way roads).


• Undirected: Edges are bidirectional (e.g., mutual friendships).

A graph can also be:

• Weighted: Edges carry a weight or cost.


• Unweighted: Edges don’t carry any cost.

🔷 Types of Graph Representation

There are two common ways to represent graphs in memory:

✅ 1. Adjacency Matrix

An Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in the graph.

• adj[i][j] = 1: There is an edge from vertex i to vertex j.


• adj[i][j] = 0: There is no edge from i to j.

For weighted graphs, we store the weight instead of 1.

🔹 Example:

Graph with 3 vertices and edges: (0→1), (1→2)

Adjacency Matrix:
012

-------
0|010
1|001

2|000
🔹 Characteristics:

• Space: O(V²) (even if the graph is sparse)


• Quick lookup to check if an edge exists
• Not efficient for sparse graphs

✅ 2. Adjacency List

An Adjacency List is an array of lists. The index represents the node, and each element in its list represents
the connected nodes.

🔹 Example:

Graph with edges: (0→1), (1→2)

Adjacency List:
0→1

1→2
2→∅

List[0] = [1]
List[1] = [2]
List[2] = []

🔹 Characteristics:

• Space: O(V + E) (efficient for sparse graphs)


• Slower to check if an edge exists (linear in list size)
• Efficient for iterating over neighbors

6. explain following terms direct graph , undirect graph , mix graph , complete graph , weight graph
, null graph , a cyclic graph , cycle , loop , isolated vertex , adjacent vertex , degree of the graph , leaf
vertex

Ans:

🔷 1. Directed Graph (Digraph)

A graph in which edges have direction.

• Each edge is an ordered pair (u, v) meaning from u → v.


• Used in one-way systems, like roads or workflows.

🧠 Example: A → B, B → C

🔷 2. Undirected Graph
A graph where edges have no direction.

• Edge (u, v) is the same as (v, u).


• Used in mutual relationships like friendship networks.

🧠 Example: A — B, B — C

🔷 3. Mixed Graph

A graph that has both directed and undirected edges.

• Some edges are one-way, and others are two-way.


• Useful in traffic maps where some roads are one-way.

🧠 Example: A → B, B — C

🔷 4. Complete Graph

A graph in which every pair of vertices is connected by a unique edge.

• For n vertices, it has n(n−1)/2 edges in undirected graphs.

🧠 Example:

For 3 vertices:

A—B

| /
C

🔷 5. Weighted Graph

A graph where edges carry weights or costs.

• Used to represent distances, costs, or capacities.


• Common in shortest path problems (Dijkstra’s Algorithm).

🧠 Example:

A —(5)— B

🔷 6. Null Graph

A graph with no edges, only isolated vertices.

• It’s the most minimal graph possible.

🧠 Example:

Vertices: A, B, C, D
Edges: None

🔷 7. Acyclic Graph

A graph that has no cycles.

• In Directed Graphs, it’s called a Directed Acyclic Graph (DAG).


• Common in task scheduling, dependency resolution.

🧠 Example:

A → B → C (no loop back to A)

🔷 8. Cycle

A closed path in a graph where the starting and ending vertex is the same and no other vertex repeats.

• Must have at least 3 vertices in undirected graphs.


• In directed graphs: all edges must follow direction.

🧠 Example:

A→B→C→A

🔷 9. Loop

An edge that starts and ends at the same vertex.

🧠 Example:

A → A or A — A

🔷 10. Isolated Vertex

A vertex that has no incident edges (i.e., no connections).

🧠 Example:

In a graph with vertices {A, B, C}, if only A and B are connected, C is isolated.

🔷 11. Adjacent Vertex

Two vertices are adjacent if there is an edge between them.

🧠 Example:

If A — B exists, then A and B are adjacent.

🔷 12. Degree of a Graph (Vertex Degree)

The number of edges connected to a vertex.


• In undirected graphs:
o Degree = number of connected edges.
• In directed graphs:
o In-degree: Edges coming into the vertex.
o Out-degree: Edges going out from the vertex.

🧠 Example:

If A → B and C → A, then:

• A: In-degree = 1, Out-degree = 1
• Degree (undirected) of B in A — B = 1

🔷 13. Leaf Vertex (Pendant Vertex)

A vertex with degree 1 — connected by only one edge.

🧠 Example:

If A is connected only to B, then A is a leaf.

7. explain graph travasal with example.

Ans:

✅ Graph Traversal in Advanced Algorithm (with C code)

Graph traversal means visiting all the vertices of a graph in a systematic way. There are two primary
traversal techniques:

🔷 1. Breadth-First Search (BFS)


• Visits neighbors level by level.
• Uses a queue.
• Ideal for finding the shortest path in unweighted graphs.

🧠 Example Graph:

0—1—2

| |

3 ———— 4

Adjacency list:

0 → 1, 3

1 → 0, 2

2 → 1, 4
3 → 0, 4
4 → 2, 3

Code for BFS:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

int visited[SIZE] = {0};


int queue[SIZE], front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) return;
if (front == -1) front = 0;
queue[++rear] = value;

int dequeue() {
if (front == -1 || front > rear) return -1;
return queue[front++];

void bfs(int graph[][SIZE], int n, int start) {


enqueue(start);

visited[start] = 1;
while (front <= rear) {
int vertex = dequeue();

prin\("%d ", vertex);


for (int i = 0; i < n; i++) {

if (graph[vertex][i] == 1 && !visited[i]) {


enqueue(i);
visited[i] = 1;
}}}}

🔷 2. Depth-First Search (DFS)


• Visits nodes deep before wide.
• Uses recursion or a stack.
• Suitable for cycle detection, topological sort, etc.

✅ C Code for DFS (using recursion):

#include <stdio.h>

#define SIZE 100


int visited[SIZE] = {0};

void dfs(int graph[][SIZE], int n, int vertex) {

visited[vertex] = 1;
prin\("%d ", vertex);
for (int i = 0; i < n; i++) {

if (graph[vertex][i] == 1 && !visited[i]) {


dfs(graph, n, i);

} } }

🔷 Sample Main Function (for both BFS & DFS)


int main() {
int graph[SIZE][SIZE] = {

{0, 1, 0, 1, 0},

{1, 0, 1, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 0, 1},
{0, 0, 1, 1, 0}

};

int n = 5;
prin\("BFS stardng from vertex 0:\n");
bfs(graph, n, 0);

// Reset visited array for DFS


for (int i = 0; i < SIZE; i++) visited[i] = 0;

prin\("\nDFS stardng from vertex 0:\n");

dfs(graph, n, 0);

return 0;

Output:
BFS stardng from vertex 0:

01324
DFS stardng from vertex 0:
01243

8. write an algorithm for DFS and BFS. (graph travasal method ) .

Ans:

Here are the algorithms and corresponding C-language implementations for DFS and BFS, commonly
used in Advanced Algorithm graph traversal.

🔷 1. Algorithm for Depth-First Search (DFS)


📘 Algorithm (Recursive DFS)

DFS(G, v):
1. Mark v as visited

2. Process v (e.g., print v)


3. For each neighbor u of v in G:

If u is not visited:
Call DFS(G, u)

✅ C Program for DFS using Adjacency Matrix


#include <stdio.h>
#define SIZE 100

int visited[SIZE];
void DFS(int graph[][SIZE], int n, int v) {

visited[v] = 1;

prin\("%d ", v);

for (int i = 0; i < n; i++) {

if (graph[v][i] == 1 && !visited[i]) {

DFS(graph, n, i);
}
}

int main() {
int n = 5;

int graph[SIZE][SIZE] = {

{0, 1, 0, 1, 0},

{1, 0, 1, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 0, 1},

{0, 0, 1, 1, 0}
};

for (int i = 0; i < SIZE; i++) visited[i] = 0;


prin\("DFS stardng from vertex 0:\n");

DFS(graph, n, 0);
return 0; }

🔷 2. Algorithm for Breadth-First Search (BFS)


📘 Algorithm (BFS using Queue)

BFS(G, start):

1. Inidalize a queue Q
2. Mark start as visited and enqueue it into Q

3. While Q is not empty:


a. Dequeue a vertex v from Q

b. Process v (e.g., print v)

c. For each neighbor u of v in G:

If u is not visited:
Mark u as visited
Enqueue u into Q

✅ C Program for BFS using Adjacency Matrix

#include <stdio.h>

#define SIZE 100

int visited[SIZE];
int queue[SIZE], front = -1, rear = -1;
void enqueue(int value) {

if (rear == SIZE - 1) return;


if (front == -1) front = 0;
queue[++rear] = value;

int dequeue() {
if (front == -1 || front > rear) return -1;
return queue[front++];
}

void BFS(int graph[][SIZE], int n, int start) {


enqueue(start);
visited[start] = 1;

while (front <= rear) {


int v = dequeue();

prin\("%d ", v);

for (int i = 0; i < n; i++) {

if (graph[v][i] == 1 && !visited[i]) {

enqueue(i);

visited[i] = 1;
}}}}
int main() {

int n = 5;

int graph[SIZE][SIZE] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 0},

{0, 1, 0, 0, 1},

{1, 0, 0, 0, 1},

{0, 0, 1, 1, 0}
};
for (int i = 0; i < SIZE; i++) visited[i] = 0;

prin\("BFS stardng from vertex 0:\n");


BFS(graph, n, 0);

return 0;

You might also like