Graph Notes
Graph Notes
Introduction
They are used to model real-world systems such as the Internet (each node represents a router and
each edge represents a connection between routers); airline connections (each node is an airport
and each edge is a flight); or a city road network (each node represents an intersection and each
edge represents a block). The wireframe drawings in computer graphics are another example of
graphs.
Types of Graph
Directed
Undirected
Weighted
A graph may be either undirected or directed. Intuitively, an undirected edge models a "two-way"
or "duplex" connection between its endpoints, while a directed edge is a one-way connection, and
is typically drawn as an arrow. A directed edge is often called an arc. Mathematically, an
undirected edge is an unordered pair of vertices, and an arc is an ordered pair.
Graphs can be classified by whether or not their edges have weights. In Weighted graph, edges
have a weight. Weight typically shows cost of traversing
Example: weights are distances between cities
GRAPH REPRESENTATIONS
There are two standard ways of maintaining a graph G in the memory of a computer.
A B C D
A 0 1 1 1
B 0 0 0 1
C 0 0 0 0
D 0 0 1 0
LINKED LIST REPRESENTATION OF GRAPH
The adjacency list is another common representation of a graph. There are many ways to
implement this adjacency representation. One way is to have the graph maintain a list of lists, in
which the first list is a list of indices corresponding to each node in the graph. Each of these refer
to another list that stores a the index of each adjacent node to this one. It might also be useful to
associate the weight of each link with the adjacent node in this list.
Example: An undirected graph contains four nodes 1, 2, 3 and 4. 1 is linked to 2 and 3. 2 is linked
to 3. 3 is linked to 4.
1 - [2, 3]
2 - [1, 3]
3 - [1, 2, 4]
4 - [3]
breadth-first
depth-first
The bread-first-search algorithm starts at a vertex and visits, first the neighbours of , then the
neighbours of the neighbours of , then the neighbours of the neighbours of the neighbours of
, and so on. This algorithm is a generalization of the breadth-first traversal algorithm for binary
trees. It uses a queue.
Algorithm_BFS
The depth-first-search algorithm is similar to the standard algorithm for traversing binary trees; it
first fully explores one subtree before returning to the current node and then exploring the other
subtree. Another way to think of depth-first-search is by saying that it is similar to breadth-first
search except that it uses a stack instead of a queue.
Algorithm_DFS
Graph:
Initial state: node 0 is pushed
Push the unvisited neighbor nodes: 8, 3, 1 (I used the reverse order to visit smaller node id first)
Next, visit the top node in the stack: 1
Push the unvisited neighbor nodes: 8 (Note: 8 is pushed again, and the previous value will be cancelled later -- as we will see
Next, visit the top node in the stack: 8
3 is visited: skip
Result:
8 is visited: skip
Result:
DONE
(The stack has become empty)
Example:
Graph:
Initial state: node 0 is enqueued
State after visiting 0
Enqueue the unvisited neighbor nodes: 1, 3, 8
Next, visit the first node in the queue: 1
State after visiting 1
Enqueue the unvisited neighbor nodes: none (Note: 4 is enqueued again, but won't be visited twice, so I leave
out)
Next, visit the first node in the queue: 7
State after visiting 7
Enqueue the unvisited neighbor nodes: none (Note: 2 is enqueued again, but won't be visited twice, so I leave
out)
Next, visit the first node in the queue: 2
State after visiting 2
Enqueue the unvisited neighbor nodes: 5
Next, visit the first node in the queue: 4
State after visiting 4