Unit 5
Unit 5
1.Types of graphs
Types of Graphs
Graphs are a fundamental data structure used to represent relationships between entities. They consist of
nodes (vertices) connected by edges. There are several types of graphs, each with its own characteristics and
applications:
1. Undirected Graphs:
Edges: Edges do not have a direction. If there's an edge between nodes A and B, it means you can go
from A to B or from B to A.
Example: Social networks, where friends are connected without specifying direction.
2. Directed Graphs:
Edges: Edges have a direction. If there's an edge from A to B, it means you can go from A to B but
not necessarily from B to A.
Example: Road networks, where roads have one-way or two-way traffic.
3. Weighted Graphs:
Edges: Each edge has an associated weight, representing a cost, distance, or other value.
Example: Road networks with distances between cities, transportation networks with costs.
4.Non-Weighted Graphs
Non-weighted graphs are graphs where the edges do not have associated weights or values. In other words,
there's no quantitative measure associated with the connections between nodes.
2.Graph terminology
Graph Terminology
Here are some common terms used in graph theory:
Nodes (Vertices):
Edges:
Degree:
Path:
Cycle:
Connected Graph:
Disconnected Graph:
Subgraph:
A subgraph that includes all nodes of the original graph and is a tree (connected and acyclic).
A spanning tree with the minimum total weight of its edges (for weighted graphs).
Graph Traversal:
The process of visiting all nodes in a graph. Common algorithms include:
o Breadth-First Search (BFS)
o Depth-First Search (DFS)
Adjacency Matrix:
A 2D matrix representing a graph, where each element indicates the existence of an edge between two
nodes.
Adjacency List:
A list representation of a graph, where each node has a list of its adjacent nodes.
3.Adjacency matrix representation of graph
Structure:
Size: The matrix is typically of size n x n, where n is the number of nodes in the graph.
Elements: Each element A[i][j] in the matrix represents the connection between nodes i and j:
o 1: If there's an edge between nodes i and j.
o 0: If there's no edge between nodes i and j.
The matrix is not necessarily symmetric. A[i][j] represents an edge from node i to node j, while
A[j][i] represents an edge from node j to node i.
Example:
A -- B -- C
\ /
D
A B C D
A 0 1 1 1
B 1 0 1 1
C 1 1 0 1
D 1 1 1 0
Can be inefficient for sparse graphs (graphs with few edges compared to the possible maximum).
Requires O(n^2) space, even for sparse graphs.
4. directed and undirected graph
Graphs are a fundamental data structure used to represent relationships between entities. They consist of
nodes (vertices) connected by edges. The type of graph depends on the directionality of its edges.
Directed Graphs
Edges: Edges have a direction, indicating a one-way relationship between the nodes.
Notation: Edges are often represented with arrows.
Example: Road networks with one-way streets, social networks with followers, and state transition
diagrams.
Undirected Graphs
Edges: Edges do not have a direction, indicating a two-way relationship between the nodes.
Notation: Edges are often represented without arrows.
Example: Friendship networks, geographic maps, and electrical circuits.