Graph Lecture (1)
Graph Lecture (1)
Lecture Flow
1) Definition of Graphs
2) Types of Graphs
3) Graph terminologies
4) Graph representations
5) Graphs & Trees
6) Receiving Inputs on Graph Problems
7) Practice questions
2
What is a 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
● If Alice is friends with Bob, Bob
is also friends with alice
Directed Graph
● 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
Different ways of representing graphs
Graph Representation
● Adjacency Matrix
● 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
int main() {
int n;
cin >> n;
vector<vector<pair<int,int>>> graph(n);
return 0;
}
Complexity Analysis
Time Complexity: O(n^2) Space
Complexity: O(n^2)
return 0;
}
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
return 0;
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
n = number of edges
Adjacency List Inputs
For Undirected Unweighted Graph
Think of ways to implement this
On Directed Weighted Graph
int main() {
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
unordered_map<int, vector<pair<int,int>>> graph;
for (int i = 0; i < n; ++i) {
string line;
getline(cin, line);
istringstream iss(line);
int node;
iss >> node; // first token is the node ID
string token;
// remaining tokens are "adj_node,weight"
while (iss >> token) {
auto pos = token.find(',');
int adj = stoi(token.substr(0, pos));
int w = stoi(token.substr(pos + 1));
graph[node].emplace_back(adj, w);
}
}
return 0;
}
Complexity Analysis
Time Complexity: O(n+m)
Space Complexity: O(2m)
n = number of nodes
m = number of edges
Common Pitfalls
Common Pitfalls
● Not considering cycles in the graph.
● 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
4. From adjacency list to adjacency matrix
5. Regular graph
6. Sources and sinks
For more graph representation Problems: Link
Leetcode Question