Você está na página 1de 26

Graphs

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

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

Shortest Path (ctd.)


Algorithm shortest path:
(INFINITY is a very large number, and MIN is the minimum value function.)

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

Linked Representation of a Graph


Let G be a directed graph with m nodes. The sequential representation in memory has major drawbacks:
It may be difficult to insert and delete nodes in G (size of array needs to be changed and nodes need to be reorderd). If the A matrix will be sparse (many zeros) a great deal of space will be wasted.
Node
A B C D E

Adjacency List
B, C, D C C, E C

(a) Graph G

(b) Adjacency list of G

Prof. L. von Dobschtz

Data Structures and Algorithms

12

Linked Representation of a Graph (ctd.)


NODE NEXT 1 2 3 4 START 4 AVAILN 5 5 6 7 8 9 10 D E B A C 3 9 8 7 1 0 2 10 6 0 1 11 6 3 0 ADJ 1 2 3 4 5 6 8 9 10 2 (C) 11 2 (C) 12
Prof. L. von Dobschtz Data Structures and Algorithms

DEST LINK 2 (C) 7 (B) 9 (D) 2 (C) 7 5 10 0 8 0 0 9 12 4 0 0


13

AVAILE 2

7 6 (E)

Operations in Graphs: Search


Suppose a graph G is maintained in memory by a linked representation and we want to find the location LOC of a node N in G. Procedure find location: FIND(NODE,NEXT,START,N,LOC) PTR:=START. Repeat while PTR <> 0: If N = NODE[PTR], then: Set LOC:=PTR, and return. Else: Set PTR:=NEXT[PTR]. Set LOC:=0, and Return.
Prof. L. von Dobschtz Data Structures and Algorithms 14

Operations in Graphs: Insert Node


Suppose a node N is to be inserted in graph G. N will be an isolated node. (The logical variable FLAG is used to indicate overflow) Precedure insert node: INSNODE(NODE,NEXT,ADJ,START,AVAILN,N,FLAG) If AVAILN=NULL, then Set FLAG:=FALSE, and Return. Set ADJ[AVAILN]:=NULL: Set NEW:=AVAILN and AVAILN:=NEXT[AVAILN]. Set NODE[NEW]:=N, NEXT[NEW]:=START, and START:=NEW. Set FLAG:=TRUE, and Return.
Prof. L. von Dobschtz Data Structures and Algorithms 15

Operations in Graphs: Insert Edge


Suppose both A and B are already nodes in the graph G. The procedure will insert the edge (A,B) in the graph G. Precedure insert edge: INSEDGE(NODE,NEXT,ADJ,START,DEST,LINK,AVAILE, A,B,FLAG) Call FIND(NODE,NEXT,START,A,LOCA). Call FIND(NODE,NEXT,START,B,LOCB). Set NEW:=AVAILE and AVAILE:=LINK[AVAILE]. Set DEST[NEW]:=LOCB, LINK[NEW]:=ADJ[LOCA] and ADJ[LOCA]:=NEW. Set FLAG:=TRUE, and Return.
Prof. L. von Dobschtz Data Structures and Algorithms 16

Operations in Graphs: Delete


Suppose a node N is to be deleted from the graph G. This operation is quite complicated because we must also delete all the edges that that contain N. The procedure will consist mainly of the following steps:
Find the location LOC of the node N in G. Delete all edges ending at N; i.e., delete LOC from the list of successors of each node M in G. Delete all edges beginning at N by finding the location BEG of the first successor and the location END of the last successor of N, and then adding the successor list of N to the free AVAILE list. Delete N itself from the list NODE.

Prof. L. von Dobschtz

Data Structures and Algorithms

17

Operations in Graphs: Delete (Ctd.)


Suppose a node N is to be deleted in graph G. Procedure delete node: DELNODE(NODE,NEXT,ADJ,START,AVAILN,DEST,LINK,AVAILE,N,FLAG) Call FIND(NODE,NEXT,START,N,LOC). If LOC=0, then: Set FLAG:=FALSE, and Return. Set PTR:=START. Repeat while PTR<>NULL: Call DELETE(DEST,LINK,ADJ[PTR], AVAILE,LOC,FLAG). Set PTR:=NEXT[PTR]. If ADJ[LOC]=NULL, then: Call DELETE(NODE,NEXT,START,AVAILN,N,FLAG), and Return. Set BEG:=ADJ[LOC], END:=ADJ[LOC] and PTR:=LINK[END]. Repeat while PTR<>NULL: Set END:=PTR and PTR:=LINK[PTR] Set LINK[END]:=AVAILE and AVAILE:=BEG. Call DELETE(NODE,NEXT,START,AVAILN,N,FLAG), and Return.
Prof. L. von Dobschtz Data Structures and Algorithms 18

Operations in Graphs: Delete (ctd.)


This procedure deletes the first node in the list containing N, or sets FLAG:=FALSE: Procedure delete: DELETE(NODE,NEXT,START,AVAILN,N,FLAG) If START=NULL, then: Set FLAG:=FALSE, and Return. If NODE[START]=N, then: Set PTR:=START, START:=NEXT[START], NEXT[PTR]:=AVAILN, AVAILN:=PTR, FLAG:=TRUE, and Return.
Prof. L. von Dobschtz Data Structures and Algorithms 19

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).

Prof. L. von Dobschtz

Data Structures and Algorithms

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

21

Breadth-First Search (ctd.)


Algorithm breadth-first search: 1. Initialize all nodes to the ready state (STATUS=1). 2. Put the starting node A in QUEUE and change its status to the waiting state (STATUS=2). 3. Repeat until QUEUE is empty:
a) Remove the front node N of QUEUE. Process N and change the status of N to the processed state (STATUS=3). b) Add to the rear of QUEUE all the neighbors of N that are in the ready state, and change their status to the waiting state.

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

Depth-First Search (ctd.)


Algorithm depth-first search: 1. Initialize all nodes to the ready state (STATUS=1). 2. Push the starting node A onto a STACK and change its status to the waiting state (STATUS=2). 3. Repeat until STACK is empty:
a) Pop the top node N of STACK. Process N and change the status of N to the processed state (STATUS=3). b) Push onto STACK all the neighbors of N that are in the ready state, and change their status to the waiting state.

4. Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 24

Example
The daily flights of an airline are as follows:

Prof. L. von Dobschtz

Data Structures and Algorithms

25

Example (ctd.)
Node List

Arrow LIst PTR


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

Atlanta Chicago Houston


1 7 2

Miami

Reno Denver Boston

9 5 3

START=2, AVAILN=5 Prof. L. von Dobschtz

AVAILE=10 Data Structures and Algorithms 26

Você também pode gostar