Graph representationWe will use an adjacency matrix represention for a graph. An example graph with its adjacency matrix is shown below. Each row (and column) of the adjacency matrix corresponds to a node in the graph. An entry (i,j) is 1 if node i is connected to node j. Note that our adjacency matrix is symmetric about the main diagonal. The diagonal entries are zero since a node is assumed not to be connected to itself. These two conditions are not strictly neccessary for general graphs but assumed here for simplicity. Each row (and column) of the adjacency matrix corresponds to a node in the graph. An entry (i,j) is 1 if node i is connected to node j.

Depth First Search (DFS)Similar to the preorder travesal for trees, we can define DFS traversal for a graph. In a graph, no special node is identified as a root. A DFS traversal can hence be initiated from any node of the graph. The figures below illustrate DFS traversals on the above graph starting from two different nodes 0 & 3.
The DFS traversal sequence initiated from node 0 is shown adjacent to each node. DFS traversal sequence initiated from node 3 is shown adjacent to each node.

An DFS application: When is a given graph a tree?A graph that satisfies following two conditions is a tree.
Both the properties can be checked by perfoming a DFS traversal on the graph,
A graph that satisifies both conditions is a tree. Examples of graphs that are also trees are shown below. The second one is in fact a onedimensional chain.

Download class files for the lab below,

http://cspeople.bu.edu/tvashwin/cs112_spring08/lab10.html 