A Graph is an ordered pair of a set
Graphs can be undirected, when each edge can be traversed in both directions, or directed, where an edge can be traversed only in one direction and is represented with an arrow.
Graphs can be weighted, where a number is associated with each edge, that can be for example the cost of traversing that specific edge.
A disconnected graph will contain nodes that cannot be reached from some other nodes, and in a fully connected graph, there is at least one edge for each node present.
Graphs can be represented in different ways, each one with its pros and cons.
- Vertex object: an object that represents a node, with an attribute like an edges list that contains all the other nodes connected.
- Edge object: an object that represents an edge, with an attribute like a node list that contains the two nodes connected by the edge.
- Edge list: a 2D list of edges represented by lists of two elements, the starting node and the ending node.
- Adjacency list: a 2D list of nodes, where at each index of the list there is a list of adjacent nodes.
- Adjacent matrix: each row and column index represent a source and a destination node, and a value of 1 in the matrix indicates that the nodes are connected.
There is some specific terminology associated with the graph:
- Node or Vertex: the basic elements of a graph containing data.
- Edge: a connection between two vertices.
- Loop: when an edge from a node is incident on itself.
- Degree of a vertex: number of vertices that are incident on a given vertex.
- Adjacency: connection of a node to its neighbour.
- Path: a sequence of vertices connected by edges.
There are two fundamental techniques for graph traversal:
- Depth First Search (DFC): where we follow one path as far as it will go.
- Breadth First Search (BFS): where we look at all the node adjacent to one before moving on to the next level.
Both techniques are often used as a basis for developing more advanced algorithms on Graphs.
DFS and BFS implementation are very similar to the one for Trees, but since there is no root node, there is no obvious place to start.
The implementation of DFS is based on a stack, and repeat the following operations:
- Start from a node and sign as visited
- Add the node to the stack
- Peek an edge, follow it to a node, and mark it as seen and add it to the stack
- as long as there are more edge and unseen nodes, we repeat that step
- when we hit a node already seen, we go back and try another edge
- if we finish the edge, we pop a node from the stack and continue from there
The runtime complexity is
Technically we visit each edge twice, one for explore and one for travelling back through it.
The implementation of BFS is based on a queue, and repeat the following operations:
- Start from a node and sign as visited
- Visit all adjacent nodes to it, and add it to the queue
- We go back to the first node, visit all its adjacent nodes and add it to the queue
We can think on BFS as we are constructing a tree, were the child of a node are all its adjacent nodes, and where we proceed to visit by levels.
The runtime complexity is