0% found this document useful (0 votes)
6 views22 pages

Week 13

The document provides an overview of graphs as a non-linear data structure consisting of vertices and edges, detailing their components, representations, and various terminologies. It explains different types of graphs, advantages and disadvantages of adjacency matrix and list representations, and common graph operations. Additionally, it introduces graph traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS).

Uploaded by

vaxshgadd
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)
6 views22 pages

Week 13

The document provides an overview of graphs as a non-linear data structure consisting of vertices and edges, detailing their components, representations, and various terminologies. It explains different types of graphs, advantages and disadvantages of adjacency matrix and list representations, and common graph operations. Additionally, it introduces graph traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS).

Uploaded by

vaxshgadd
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/ 22

DATA STRUCTURES

CSO102
WEEK-13 (DRAFT)
GRAPH:
- a non-linear data structure consisting of vertices and edges
- the vertices are sometimes also referred to as nodes
- the edges are lines or arcs that connect any two nodes in the graph

More formally, a graph is composed of a set of vertices( V ) and a set


of edges( E ). The graph is denoted by G(E, V)
Components of a graph:
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also
known as nodes. Every node/vertex can be labeled or unlabelled.

Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered
pair of nodes in a directed graph. Edges can connect any two nodes in any possible way.
There are no rules. Sometimes, edges are also known as arcs. Every edge can be
labeled/unlabelled.
Representations of graphs:

● Adjacency Matrix
● Adjacency List
Adjacency Matrix Representation:
Adjacency List Representation:
Terminologies:
Path

A path can be defined as the sequence of nodes that are followed in order to reach some
terminal node V from the initial node U.

Closed Path

A path will be called as closed path if the initial node is same as terminal node. A path will be
closed path if V0=VN.

Simple Path

If all the nodes of the graph are distinct with an exception V0=VN, then such path P is called
as closed simple path.
Terminologies:
Cycle

A cycle can be defined as the path which has no repeated edges or vertices except the first and
last vertices.

Connected Graph

A connected graph is the one in which some path exists between every two vertices (u, v) in V.
There are no isolated nodes in connected graph.

Complete Graph

A complete graph is the one in which every node is connected with all other nodes. A complete
graph contain n(n-1)/2 edges where n is the number of nodes in the graph.
Terminologies:
Weighted Graph

In a weighted graph, each edge is assigned with some data such as length or weight. The
weight of an edge e can be given as w(e) which must be a positive (+) value indicating the cost
of traversing the edge.

Adjacent Nodes

If two nodes u and v are connected via an edge e, then the nodes u and v are called as
neighbours or adjacent nodes.

Degree of the Node

A degree of a node is the number of edges that are connected with that node. A node with
degree 0 is called as isolated node.
Types of graphs:
Null Graph

Non-directed Graph

Directed Graph

Connected Graph

Disconnected Graph

Regular Graph

Complete Graph

Cyclic Graph

Acyclic Graph
Types of graphs:
Finite Graph

Infinite Graph

Bipartite Graph

Planar Graph

Simple Graph

Multi Graph

Euler Graph

Hamiltonian Graph
Adjacency Matrix:

Advantages of Adjacency Matrix:


● Representation is easier to implement and follow.
● Removing an edge takes O(1) time.
● Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can
be done O(1).

Disadvantages of Adjacency Matrix:

● Consumes more space O(V2) . Even if the graph is sparse(contains less number of

edges), it consumes the same space.


● Computing all neighbors of a vertex takes O(V) time (Not efficient).
Adjacency List:

Advantages of Adjacency List:


● Saves space. Space taken is O(|V|+|E|). In the worst case, there can be VC2 number of

edges in a graph thus consuming O(V2) space.

● Adding a vertex is easier.


● Computing all neighbors of a vertex takes optimal time.

Disadvantages of Adjacency List:


Queries like whether there is an edge from vertex u to vertex v are not efficient and can be
done O(V).
Graph Operations:

The most common graph operations are:

● Check if the element is present in the graph


● Add elements(vertex, edges) to graph
● Finding the path from one vertex to another
● Graph Traversal
Graph Traversal Techniques:

DFS (Depth First Search)

BFS ( Breadth First Search)


Depth First Search:
Depth First Search Tree:
Depth First Search:
Depth First Search:
TIME COMPLEXITY ?
Breadth First Search:
Breadth First Search:
Breadth First Search:
TIME COMPLEXITY ?

You might also like