CIS 3223. Data Structures and Algorithms

Graphs (1)

 

1. Basic concepts

Intuitively, a graph represents a network of relations among data items, in which each data item can be related to many other items. Graph is a very flexible data structure that can be used for various purposes.

Formally, a graph G consists of a set V whose members are the vertices (nodes) of G, together with a set E of pairs of vertices from V. These pairs are called edges (or arcs, links) of G.

The size of each graph is measured by |V| and |E|, where 0 ≤ |V|, 0 ≤ |E| ≤ |V2|.

Linear and tree data structures are special cases of graph.

For example, we can have G = < V, E >, where V = {1, 2, 3, 4, 5} and E = {(1, 2), (2, 3), (3, 4), (4, 5), (1, 5), (3, 5), (2, 4)}. The graph looks like:

                        1---2---3
                         \   \ /|
                          \   X |
                           \ / \|
                            5---4
Please note that the same graph can be drawn in many ways.

Based on the edges, we can further define the following notions:

If the pairs comprising an edge are unordered, i.e. (1, 2) or (2, 1) represent the same edge, the graph is called an undirected graph . If the pairs are ordered, the graph is a directed graph or digraph. To draw such a graph, it is necessary to mark the direction of the edges with arrows. Also, the above definitions are slightly different in directed graphs, since in a directed graph, we must follow the direction of the arrows to form a path. In a digraph, vertex A is adjacent to vertex B only if there is a directed edge from A to B (some books use the term in the opposite direction).

Then we can talk about connectivity:

Now we can define a tree as a weakly connected digraph with a unique vertex (the root) from which there is a unique path to each of the other vertices.

A weighted graph has a number attached to each edge, to represent some kind of "distance". In such a graph, the weight of a path can be defined as the sum of the weights on the path.

 

2. Storage structure

To represent a graph within a program, you can specify each vertex and the set of vertices adjacent to it.

Example: vertex set of adjacent vertices is the following:

0 {1, 2}
1 {2, 3}
2 {}
3 {0, 1, 2}
The above must be a digraph, because 0 is adjacent to 1 but 1 is not adjacent to 0. The graph looks like the following (with '+' for arrowhead):
       0----+1
       |+   /+
       | \ / |
       |  X  |
       | / \ |
       ++   \+
       2+----3
One way to represent a graph is as an array of linked lists:
0 --> 1 --> 2
1 --> 2 --> 3
2
3 --> 0 --> 1 --> 2
In general, the vertices in each list are not necessarily sorted.

Another way to represent a graph is to use an adjacency matrix of boolean values to represent a graph. If the graph contains an edge from 0 to 1, then row 0, column 1 should be True. For instance, the above graph can be represented in the following matrix:

\ 0 1 2 3
0 F T T F
1 F F T T
2 F F F F
3 T T T F

In both implementations, if the vertices are not sequentially numbered, but labeled in another way, some kind of mapping between the two will be needed.

Complexity in terms of |V| and |E|:

A Java implementation of graph is in the textbook.