Lecture 1(Graphs Learning)¶
Types of data structures¶
- Linear : array , linked list , stack , queue
- Non Linear : Tree , Graphs
Graphs¶
- Node (data ) connected via edges
- Edges : Directed and un directed
- nodes (order is not important)
- Graphs may or may not have cycle
- If graph has atleast one cycle is called undirected/directed cyclic graph
- Path : list of nodes [ start to end] connected via edges
- Degree of node : number of edges connected to a node
- Total degree of graph = 2 X (number of edges)
- Edge weight : edges might have weight also known as cost of an edge (by default it is 1)
Graph representation¶
1 3
1 2
3 4
3 5
2 4
4 5
Approach 1 Adjacency Matrix¶
- number of nodes : 5 (1,2,3,4,5)
- number of edges : 6
- array [n+1 ][n+1] : 0 based indexing
In [ ]:
graph = [
[0,0,0,0,0,0],
[0,0,1,1,0,0],
[0,1,0,1,0,0],
[0,1,0,0,1,1],
[0,0,1,1,0,1],
[0,0,0,1,1,0]
]
Space Needed¶
- O(N^2)
Approach 2 Adjaceny List¶
- number of nodes : 5 (1,2,3,4,5)
- number of edges : 6
- array [n+1 ] : 0 based indexing
In [ ]:
graph = [[1,2],[1,3],[2,4],[3,4],[3,5],[4,5]]