forked from techhubmvit/dataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphbasics
21 lines (21 loc) · 1.75 KB
/
graphbasics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
A graph data structure is a collection of nodes that have data and are connected to other nodes. It is a non-linear data structure.
They consists of nodes and edges. Nodes are also called as vertices and the edges are lines that connect these vertices.
Vertices represnted as(v) and edges together represented as (uv).
Graph Terminology:
Adjacency: A vertex is said to be adjacent to another vertex if there is an edge connecting them. Vertices 2 and 3 are not adjacent because there is no edge between them.
Path: A sequence of edges that allows you to go from vertex A to vertex B is called a path. 0-1, 1-2 and 0-2 are paths from vertex 0 to vertex 2.
Directed Graph: A graph in which an edge (u,v) doesn't necessary mean that there is an edge (v, u) as well. The edges in such a graph are represented by arrows to show the direction of the edge.
Graph represtation:
Graphs are repsented in 2 ways
1. Adjacency Matrix
Using a 2D array where each row and column represnt a vertex.
If the value of any element a[i][j] is 1, it represents that there is an edge connecting vertex i and vertex j.
Else if it is 0 then there is no link.
2. Adjacency List
An adjacency list represents a graph as an array of linked list.
The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex.
Graphs Use:
Graphs are used to solve many real-life problems. Graphs are used to represent networks.
The networks may include paths in a city or telephone network or circuit network.
Graphs are also used in social networks like linkedIn, Facebook. For example, in Facebook, each person is represented with a vertex(or node).
Each node is a structure and contains information like person id, name, gender, locale etc.