This repository is dedicated to Python data structures and algorithms for learning purposes. This youtube video (https://www.youtube.com/watch?v=tWVWeAqZ0WU) by Alvin Zablan is an awesome resource to learn graph algorithms specifically. Alvin explains the approach to a problem fluently and makes graph solution implementation like a piece of cake.
What is a graph?
Graph = Nodes + Edges
Where a, b, c, d, e, and f are nodes and edges are the connections between the nodes. Nodes are vertices are interchangeably used. There can be many edges between the nodes.
Graphs can be cyclic and/or acyclic where:
cyclic = A path in the nodes where we can get back where we started from.
acyclic = Where there is no such path. Or in other words there is only a certain direction to follow.
Cyclic and acyclic are also known as directed and undirected, respectively. However, graphs can have cycle as well as a specific direction within it's nodes.
Breadth First Traversal
BFS uses queue data structure where we add from the back and remove from the front. It uses FIFO concept, first-in-first-out. While DFS uses stack data structure and is based on LIFO concept. We always remove the last element we added.
A BFS traversal will explore nodes in all the directions evenly. As show here
Important BFS is implemented iteratively and DFS is implemented recursively. The reason for that is because BFS uses a queue data structure which has an inherent recursive stack call.
Graph Matrix
In a graph, any position can be accessed using (row, col) and any position will at least have four neighbors. Up, down, left and right directions. Let's say we are at a position (r,c), and if we wanted to go up we are reducing the row by one, so up direction will be represented by (r-1,c). Similarly, if we wanted to go down we are increasing the row, so incrementing r by will give the down neighbor (r+1,c). Likewise, if wanted a neighbor to the right, we'd increment the col position by one, (r, c+1) and left will be represented by (r, c-1). These are the four direction shown in the diagram below.
What is the difference between a graph and tree?
A graph data structure may not have a node, and there maybe multiple paths between nodes. However, in a tree there is a single unique path between two nodes.
Is an adjacency list the only way to implement a graph?
No, graph representations are diverse. We can have edge-lists, adjacency-lists, grid-graphs etc.
What is a tree?
- Tree has a root node, and bears a parent child relationship between different nodes
- Root node has no parent. While leaf nodes have no children.
What is a binary tree?
- A binary tree is a tree where every child has atmost two children
- A node in a binary tree could have less than two children
- Has exactly one root node
- exactly one path between root and any node