Escolar Documentos
Profissional Documentos
Cultura Documentos
CONTENTS
- Introduction and Terminology - Multigraphs - Directed Graphs - Representation of Graphs - Path Matrix, Shortest Paths - Operations: Search, Insert, Delete - Traversing: Breadth-First and Depth-First Search
Terminology (1)
A graph G(V,E) consists of two things:
A set V of elements called nodes (or points). A set E of edges such that each edge e in E is identified with a unique unordered pair [u,v] of nodes in V, denoted e = [u,v].
Suppose e = [u,v]. Then the nodes u and v are called the endpoint of e, and u and v are said to be the adjacent nodes or neighbors. The degree of a node u, deg(u), is the number of edges containing u. If deg(u)=0, u is called an isolated node.
Terminology (2)
A path P of length n from a node n to a node v is defined as a sequence of n+1 nodes P=(v0, v1, v2, , vn). The path is said to be closed if v0=vn. The path is called simple if all nodes are distinct, with the exception that v0 may equal vn. A cycle is a closed simple path with length 3 or more. A graph G is connected if there is a path between any two of its nodes.
A B C
D
Prof. L. von Dobschtz
E
Data Structures and Algorithms
F
3
Terminology (3)
A graph G is said to be complete if every node u in G is adjacent to every other node v in G. Clearly such a graph is connected. A complete graph with n nodes will have n(n-1)/2 edges. A connected graph without any cycles is called a tree. This means,there is a unique simple path P between any two nodes u and v in T. A graph G is said to be labeled if its edges are assigned data. In particular, G is said to be weighted if each edge e in G is assigned a non negative numerical value w(e) called the weight or length of e. In such a case, each path P in G is assigned a weight or length which is the sum of the weights of the edges along the path P.
Multigraphs
The definition of a graph usually does not allow multiple edges or loops. The definition of a graph, however, may be generalized by permitting the following:
Multiple edges: Distinct edges e and e' are called multiple edges if they connect the same endpoints. Loops: An edge e is called a loop if it has identical endpoints.
Such a generalization is called a multigraph M. A multigraph is said to be finite if it has a finite number of nodes and edges.
Prof. L. von Dobschtz Data Structures and Algorithms 5
Directed Graphs
A multigraph is called directed graph G (digraph) if each edge e in G is assigned a direction, i.e. edge e is identified with an ordered pair (u,v) of nodes instead of [u,v]. The following terminology is used:
e begins at u and ends at v. u is the origin of e, and v its destination. u is a predecessor of v, and v the successor of u.
The outdegree of a node u in G is the number of edges beginning at u, and the indegree the number of edges ending at u. A node u is called a source if it has a positive outdegree but zero indegree, u is called a sink if it has a zero outdegree but a positive indegree.
Prof. L. von Dobschtz Data Structures and Algorithms 6
Representation of Graphs
There are two standard ways of maintaining a graph G in the memory of the computer:
The sequential representation of G by means of its adjacency matrix. The linked representation of G by means of linked lists of neighbours.
Suppose G is a simple directed graph with m nodes and the nodes of G have been ordered and are called v1, v2, , vm. Then the adjacency matrix A=(aij) of G is the mxm matrix
aij= 1, if there is an edge (vi,vj). aij= 0, otherwise.
Such a matrix A, which contains entries of only 0 and 1, is called a bit or a Boolean matrix.
Prof. L. von Dobschtz Data Structures and Algorithms 7
Path Matrix
Suppose G is a simple directed graph with m nodes and the nodes of G have been ordered and are called v0, v1, v2, , vm. Then the path or reachability matrix P=(pij) of G is the m-square matrix
pij= 1, if there is a path from vi to vj. pij= 0, otherwise.
The adjacency matrix A and the path matrix P of a graph may be viewed as logical matrices, where 0 represents false and 1 represents true.
Example
X X A= Y Z W 0 1 1 0 Y 0 0 0 0 Z 0 1 0 1 W 1 1 1 0
X X P= Y Z W
Prof. L. von Dobschtz Data Structures and Algorithms
Y 0 0 0 0
Z 1 1 1 1
W 1 1 1 1
9
1 1 1 1
Shortest Paths
Let G be a directed graph with m nodes, v1, v2, , vm. Suppose G is weighted. Then G may be maintained in memory by its weight matrix W=(wij), defined as follows:
wij= w(e), if there is an edge (vi,vj). wij= 0, otherwise.
Now we want to find a matrix Q which will tell us the length of the shortest paths between the nodes, or a matrix Q=(qij) where qij = length of shortest path from vi to vj.
Prof. L. von Dobschtz Data Structures and Algorithms 10
Repeat for I,J = 1 to M: If W[I,J]=0, then: Set Q[I,J]:=INFINITY. Else: Set Q[I,J]:=W(I,J). Repeat for K=1 to M: Repeat for I=1 BIS M: Repeat for J=1 to M: Set Q[I,J]:=MIN(Q[I,J],Q[I,K]+Q[K,J]). Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 11
Adjacency List
B, C, D C C, E C
(a) Graph G
12
AVAILE 2
7 6 (E)
17
Traversing a Graph
Many graph algorithms require one to systematically examine the nodes and edges of a graph G. There are two standard ways to do it:
The breadth-first search will use a queue as an auxiliary structure. The depth-first search will use a stack.
During the execution each node of G will be in one of three states, called the status of N:
Status=1: The initial state of the node N (ready state). Status=2: The node N is on the queue or stack, waiting to be processed (waiting state). Status=3: The node has been processed (processed state).
20
Breadth-First Search
The general idea behind a B-F search beginning at a starting node A is:
First we examine the starting node A. Then we examine all the neighbors of A. Then we examine all the neighbors of the neighbors of A.
We need to keep track of the neighbors of a node, and need to guarantee that no node is processed more than once. This is accomplished by using a queue to hold nodes that are waiting to be processed, and by using a field STATUS which tells us the current status of each node.
21
4. Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 22
Depth-First Search
The general idea behind a D-F search beginning at a starting node A is:
First we examine the starting node A. Then we examine each node N along a path P which begins at A (i.e. we process a neighbor of A, then a neighbor of a neighbor of A, etc.). When we come to the end of P, we backtrack on P until we can continue along another path P', etc..
The algorithm is very similar to the B-F search except now we use a stack instead of a queue. A field STATUS is used to tell us the current status of a node..
Prof. L. von Dobschtz Data Structures and Algorithms 23
4. Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 24
Example
The daily flights of an airline are as follows:
25
Example (ctd.)
Node List
CITY
NEXT 0 12 11 7 6 8 10 9 1 0 4 3
NUMBER 103 106 201 203 204 301 305 308 402
ORIG 2 4 12 12 11 11 3 7 10
DEST 4 2 3 11 12 10 7 12 3
LINK 0 0 4 0 6 0 0 0 0 11 12 0
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
1 7 2
Miami
9 5 3