A2SV Graph Lecture
A2SV Graph Lecture
What is graph?
Definition
● A way to represent relationships between objects.
● A collection of nodes that have data and are connected to other
nodes.
Example: Friendship Graph
● Nodes or vertices:
The objects in graph
○ These people
our case
● Edges: the relation
between nodes.
○ The friendship
between them
(the lines)
What are the types of graphs?
Undirected Graph
● Facebook friendship
● In directed graphs:
○ Indegree = edges
ending at node.
○ Outdegree = edges
starting at node.
Path
● A path leads from one node to another.
A path is a cycle if the first and the last node of the path is the same.
Connectivity
● A graph is connected if there is a path between any two nodes
Components
The connected parts of a graph are called its components
Complete Graph
A complete graph is a graph in which each pair of node is connected by an edge.
Summary
Common Terminologies
Node: _____?
Edge: _____?
Path: _____?
Cycle: _____?
Connectivity: _____?
Components: _____?
Common Terminologies
Node: represents elements
Neighbours(adjacent): _____?
Degree: _____?
Indegree: _____?
Outdegree: _____?
Traverse: _____?
Common Terminologies
Tree: is a connected graph consists of n nodes and n-1 edges
Complete Graph: every node has n-1 degree(an edge from every node to every other node)
● Adjacency List
● Edge List
● Grids as Graphs
Adjacency matrix
Advantages and disadvantages of
adjacency matrix?
Advantages: Disadvantage:
○ 4 perpendicular
○ 2 diagonal/antidiagonal.
Direction vectors
Graph and Tree
Tree
● A tree is a connected and acyclic graph.
● A tree has a unique path between any two vertices.
● How many edges does a tree have?
Receiving Inputs on Graph Problems
Adjacency Matrix Inputs
On Directed Weighted Graphs
Code
from collections import defaultdict
n = int(input())
graph = defaultdict(list)
for i in range(n):
row = list(map(int, input().split()))
for j in range(len(row)):
graph[i].append((j, row[j]))
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
n = int(input())
graph = defaultdict(list)
for i in range(n):
row = list(map(int, input().split()))
for j in range(len(row)):
graph[i].append((j, row[j]))
graph[j].append((i, row[i]))
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
n = int(input())
graph = defaultdict(list)
for i in range(n):
Src, dest, w = list(map(int, input().split()))
Graph[src].append((dest, w))
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(2n)
n = number of edges
Another Form of Edges List
Adjacency List Inputs
On Directed Weighted Graph
Code
from collections import defaultdict
n = int(input())
graph = defaultdict(list)
for i in range(n):
line = input().split()
node = int(line[0])
graph[node].append((adj_node, weight))
Complexity Analysis
Time Complexity: O(n+m)
Space Complexity: O(2m)
n = number of nodes
m = number of edges
For Multiple Graph Problems
Which kind of input is the graph?
OR
To make your inputs faster…
● Dijkstra's algorithm
● Bellman-Ford algorithm
● Kruskal's algorithm
● Floyd-Warshall algorithm
1. Operations on graph
2. Cities and roads
3. From adjacency matrix to adjacency list
5. Regular graph
6. Sources and sinks
For more graph representation Problems: Link
Quote of the day
“Slow Success builds character.
Fast Success builds ego.”
~ Unknown