Click here for the slides presentation.
|Introduction to Graphs|
Graphs are the most general data structure. They are also commonly used data
Undirected graph definition:
In a drawing of a graph, the placement of the vertices and edges is unimportant.
A Motivating Example: Coin Game (Undirected State Graphs)
Graphs in Problem Solving:
To solve the above problem, we are going to build an undirected state graph. See Figure 14.1 on page 703 of our text book. Once we know the undirected state graph, the game becomes a problem of finding a path from one vertex to another, where the path is allowed only to follow edges.
One application of directed graphs is a state graph for a game where reversing a move is sometimes forbidden. For example, tic-tac-toe.
More Graph Terminology:
Airline Routing Example: Crocodile Airlines Routes
Suppose we have 4 coins in the coin game.
Your mission: Draw the directed state graph for this game and determine whether it is possible to go from the start to the goal. Why does the graph need to be directed?
Different kinds of graphs require different kinds of implementations, but the
fundamental concepts of all graph implementations are similar. We'll look at
several representations for one particular kind of graph: directed graphs in
which loops are allowed.
Representing Graphs with an Adjacency Matrix
We can use a two-dimensional array to store an adjacency matrix:
boolean adjacent = new boolean;
Once the adjacency matrix has been set, an application can examine locations of the matrix to determine which edges are present and which are missing.
Representing Graphs with Edge Lists
Loops and multiple edges could be allowed.
Representing Graphs with Edge Sets
To represent a graph with n vertices, we can declare an array of n sets of integers. For example:
IntSet connections = new IntSet; // 10 vertices
A set such as connections[i] contains the vertex numbers of all the vertices to which vertex i is connected.
Which Representation is Best?
If the space is available, then an adjacency matrix is easier to implement and is generally easier to use than edge lists or edge sets.
There are also other considerations:
In general, your choice of representations should be based on your expectations as to which operations are most frequent. One last consideration is the average number of edges originating at a vertex. If each vertex has only a few edges (sparse graph), then an adjacency matrix is mostly wasted space filled with the value false.
|Graph Implementation (Directed Graphs with no Multiple Edges)|
There are two common ways of traversing a graph:
The same underlying purpose:
Note: a traversal processes only those vertices that can be reached from the start vertex.
Note: the algorithm must not enter a repetitive cycle. To prevent this, the algorithm needs to mark each vertex as it is processed.
DFS can be used for traversing or searching a tree. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking. Formally, DFS progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hasn't finished exploring.
Similarly, BFS can also be used for traversing or searching a tree. The BFS begins at a root node and searches all the children nodes. Then for each of these children nodes in turn, it inspects their children which were not visited, and so on. The process will continue until a goal node is found or it hits nodes with no children.
After studying this chapter, you should be able to: