Sei sulla pagina 1di 55

Graphs

Graph G(V,E): finite set V of nodes


(vertices) plus a finite set E of arcs (or
edges) between nodes
V = {A, B, C, D, E, F, G}
E = {AA, AB, AD, CD, DE, BF, EF, FG, EG }
B
A

F
C

D
E.G.M. Petrakis

E
Graphs

Terminology
Two vertices are adjacent if they are
connected with an edge
Adjacent or neighbor vertices
Subgraph of G=(V,E): a graph G=(V,E)
where V, E are subsets of V, E
|V|,|E|: number of nodes, edges of G
Complete graph: contains all possible edges
|E| in (|V|2)
E.G.M. Petrakis

Graphs

Undirected Graphs
B
C

G
H

The edges are not directed from one


vertex to another
E.G.M. Petrakis

Graphs

Directed Graphs
B
C

G
H

The edges are directed from one


vertex to another
E.G.M. Petrakis

Graphs

A
B
D

A
C

C
D

F
G

E.G.M. Petrakis

Graphs

More Definitions
Vertex degree: maximum number of
incoming or outgoing edges
in-degree: number of incoming edges
out-degree: number outgoing edges

Relation R on A: R = {<x, y>| x, y N}


x<y
x mod y = odd

Labeled graph: labels on vertices or edges


Weighted graph: weights on edges
E.G.M. Petrakis

Graphs

relation
3

weighted

10

10
7

17

E.G.M. Petrakis

17

Graphs

Paths
There exist edges connecting two nodes
Length of path: number of edges
Weighted graphs: sum of weights on path
Cycle: path (of length 3 or more)
connecting a vertex with itself
Cyclic graph: contains cycle
Acyclic graph: does not contain cycle
DAG: Directed Acyclic Graph

E.G.M. Petrakis

Graphs

Connected Graphs
0
4
1

An undirected graph is connected if


there is a path between any two
vertices
Connected components: maximally
connected subgraphs
E.G.M. Petrakis

Graphs

Operations on Graphs

join (a,b)
a
join (a,b,x)
a
remv[wt] (a,b,x)
adjacent (a,b) a
a

x
a

b
b
b

adjacent (a,b) = true


adjacent (a,c) = false

c
E.G.M. Petrakis

Graphs

10

Build a Graph
read(n);
read and create n nodes
label them from 1 .. n

// number of nodes

while not eof(..)


{
read(node1, node2, w12);
joinwt(node1, node2, w12);
}
any graph algorithm
E.G.M. Petrakis

Graphs

11

Representation of Directed Graphs


directed graph
0

0
1
2
3
4

4
1

E.G.M. Petrakis

2 3 4

1
1

adjacency matrix

adjacency list
Graphs

12

Representation of Undirected Graphs


undirected graph
0

0
1
2
3
4

4
1

E.G.M. Petrakis

1
1
1

1
1

3 4
1
1 1
1 1

adjacency matrix
4

2
Graphs

adjacency list
13

Matrix Implementation
Simple but difficult to process edges,
unused space in matrix
mark: 1D array marking vertices
mark[i] = 1 if vertex i has been visited
typedef int *Edge;
matrix: the actual Graph implemented as an
1D array of size n2
edge (i, j) is found at matrix[i*n + j]
if edge exists matrix[i*n + j] = wt
otherwise matrix[i*n + j] = NOEDGE
E.G.M. Petrakis

Graphs

14

Graph Interface
interface Graph {
public int n();
public int e();
public Edge first(int v);
public Edge next(Edge w);
public boolean isEdge(Edge w);
public boolean isEdge(int i, int j);
public int v1(Edge w);
public int v2(Edge w);
public void setEdge(int i, int j, int weight);
public void setEdge(Edge w, int weight);
public void delEdge(Edge w);
public void delEdge(int i, int j);
public int weight(int i, int j);
public int weight(Edge w);
public void setMark(int v, int val);
public int getMark(int v);
} // interface Graph

E.G.M. Petrakis

Graphs

// Graph class ADT


// Number of vertices
// Number of edges
// Get first edge for vertex
// Get next edge for a vertex
// True if this is an edge
// True if this is an edge
// Where edge came from
// Where edge goes to
// Set edge weight
// Set edge weight
// Delete edge w
// Delete edge (i, j)
// Return weight of edge
// Return weight of edge
// Set Mark for v
// Get Mark for v
15

Implementation: Edge Class


interface Edge {
public int v1();
public int v2();
} // interface Edge

// Interface for graph edges


// Return the vertex it comes from
// Return the vertex it goes to

// Edge class for Adjacency Matrix graph representation


class Edgem implements Edge {
private int vert1, vert2; // The vertex indices
public Edgem(int vt1, int vt2)
{ vert1 = vt1; vert2 = vt2; }
public int v1() { return vert1; }
public int v2() { return vert2; }
} // class Edgem
E.G.M. Petrakis

Graphs

16

Graph class: Adjacency Matrix


class Graphm implements Graph { // Graph: Adjacency matrix
private int[][] matrix;
// The edge matrix
private int numEdge;
// Number of edges
public int[] Mark;
// The mark array
public Graphm(int n) {
Mark = new int[n];
matrix = new int[n][n];
numEdge = 0;
}

// Constructor

public int n() { return Mark.length; }


public int e() { return numEdge; }
E.G.M. Petrakis

Graphs

// Number of vertices
// Number of edges
17

Adjacency Matrix (cont.)


public Edge first(int v) {
// Get the first edge for a vertex
for (int i=0; i<Mark.length; i++)
if (matrix[v][i] != 0)
return new Edgem(v, i);
return null; // No edge for this vertex
}
public Edge next(Edge w) { // Get next edge for a vertex
if (w == null) return null;
for (int i=w.v2()+1; i<Mark.length; i++)
if (matrix[w.v1()][i] != 0)
return new Edgem(w.v1(), i);
return null; // No next edge;
}
E.G.M. Petrakis

Graphs

18

Adjacency Matrix (cont.)


public boolean isEdge(Edge w) {
// True if this is an edge
if (w == null) return false;
else return matrix[w.v1()][w.v2()] != 0;
}
public boolean isEdge(int i, int j)
{ return matrix[i][j] != 0; }

// True if this is an edge

public int v1(Edge w)


{ return w.v1(); }

// Where edge comes from

public int v2(Edge w)


{ return w.v2(); }

// Where edge goes to

E.G.M. Petrakis

Graphs

19

Adjacency Matrix (cont.)


// Set edge weight
public void setEdge(int i, int j, int wt) {
Assert.notFalse(wt!=0, "Cannot set weight to 0");
matrix[i][j] = wt;
numEdge++;
}
// Set edge weight
public void setEdge(Edge w, int weight) {
if (w != null)
setEdge(w.v1(), w.v2(), weight);
}

E.G.M. Petrakis

Graphs

20

Adjacency Matrix (cont.)


public void delEdge(Edge w) {
if (w != null)
if (matrix[w.v1()][w.v2()] != 0) {
matrix[w.v1()][w.v2()] = 0;
numEdge--;
}
}

// Delete edge w

public void delEdge(int i, int j) {


if (matrix[i][j] != 0) {
matrix[i][j] = 0;
numEdge--;
}
}

// Delete edge (i, j)

E.G.M. Petrakis

Graphs

21

Adjacency Matrix (cont.)


public int weight(int i, int j) {
// Return weight of edge
if (matrix[i][j] == 0) return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w) {
// Return weight of edge
Assert.notNull(w, "Can't take weight of null edge");
if (matrix[w.v1()][w.v2()] == 0) return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v, int val) { Mark[v] = val; }
public int getMark(int v) { return Mark[v]; }

// Set Mark
// Get Mark

} // class Graphm
E.G.M. Petrakis

Graphs

22

Implementation: Edge Class


// Edge class for Adjacency List graph representation
class Edgel implements Edge {
private int vert1, vert2; // Indices of v1, v2
private Link itself; // Pointer to node in adj list
public Edgel(int vt1, int vt2, Link it) //Constructor
{ vert1 = vt1; vert2 = vt2; itself = it; }
public int v1() { return vert1; }
public int v2() { return vert2; }
Link theLink() { return itself; } // Access adj list
} // class Edgel
E.G.M. Petrakis

Graphs

23

Graph Class: Adjacency List


class Graphl implements Graph {
private GraphList[] vertex;
private int numEdge;
public int[] Mark;
public Graphl(int n) {
Mark = new int[n];
vertex = new GraphList[n];
for (int i=0; i<n; i++)
vertex[i] = new GraphList();
numEdge = 0;
}
E.G.M. Petrakis

Graphs

// Graph: Adjacency list


// The vertex list
// Number of edges
// The mark array
// Constructor

24

Adjacency List (cont.)


public int n() { return Mark.length; } // Number of vertices
public int e() { return numEdge; }

// Number of edges

public Edge first(int v) { // Get the first edge for a vertex


vertex[v].setFirst();
if (vertex[v].currValue() == null) return null;
return new Edgel(v, ((int[])vertex[v].currValue())[0],
vertex[v].currLink());
}

E.G.M. Petrakis

Graphs

25

Adjacency List (cont.)


public boolean isEdge(Edge e) { // True if this is an edge
if (e == null) return false;
vertex[e.v1()].setCurr(((Edgel)e).theLink());
if (!vertex[e.v1()].isInList()) return false;
return ((int[])vertex[e.v1()].currValue())[0]
== e.v2();
110,60
}
public boolean isEdge(int i, int j) { // True if this is an edge
GraphList temp = vertex[i];
for (temp.setFirst(); ((temp.currValue() != null) &&
(((int[])temp.currValue())[0] < j)); temp.next());
return (temp.currValue() != null) &&
(((int[])temp.currValue())[0] == j);
}
E.G.M. Petrakis

Graphs

26

Adjacency List (cont.)


public int v1(Edge e) { return e.v1(); } // Where edge comes from
public int v2(Edge e) { return e.v2(); } // Where edge goes to
public Edge next(Edge e) {
// Get next edge for a vertex
110,60
vertex[e.v1()].setCurr(((Edgel)e).theLink());
vertex[e.v1()].next();
if (vertex[e.v1()].currValue() == null) return null;
return new Edgel(e.v1(), ((int[])vertex[e.v1()].currValue())[0],
vertex[e.v1()].currLink());
}

E.G.M. Petrakis

Graphs

27

Adjacency List (cont.)


public void setEdge(int i, int j, int weight) {// Set edge weight
Assert.notFalse(weight!=0, "Cannot set weight to 0");
int[] currEdge = { j, weight };
if (isEdge(i, j))
// Edge already exists in graph
vertex[i].setValue(currEdge);
else {
// Add new edge to graph
vertex[i].insert(currEdge);
numEdge++;
}
}
public void setEdge(Edge w, int weight) // Set edge weight
{ if (w != null) setEdge(w.v1(), w.v2(), weight); }
E.G.M. Petrakis

Graphs

28

Adjacency List (cont.)


public int weight(int i, int j) { // Return weight of edge
if (isEdge(i, j)) return ((int[])vertex[i].currValue())[1];
else return Integer.MAX_VALUE;
}
public int weight(Edge e) { // Return weight of edge
if (isEdge(e)) return ((int[])vertex[e.v1()].currValue())[1];
else return Integer.MAX_VALUE;
}
public void setMark(int v, int val) { Mark[v] = val; } // Set Mark
public int getMark(int v) { return Mark[v]; }
// Get Mark
} // class Graphl
E.G.M. Petrakis

Graphs

29

A Better Implementation
arcptr

info nextnode

ndptr

info: data
arcptr: pointer to an adjacent
node
nextnode: pointer to a graph
B
node

ndptr: pointer to adjacent node


nextarc: pointer to next edge

D
C

E.G.M. Petrakis

nextarc

Graphs

30

graph
A

n
i
l

n
i
l

n
i
l

n
i
l
<D,B>

n
i
l
<C,E>

n
i
l
<A,B>

E.G.M. Petrakis

<A,C>

<A,D>

Graphs

<A,E>

31

Graph Traversal
Trees
preorder
inorder
postorder
Graphs
Depth First Search (DFS)
Breadth First Search (BFS)
E.G.M. Petrakis

Graphs

32

Depth First Search (DFS)


Starting from vertex v, initially all
vertices are unvisited

void DFS(v) {
Mark(v) = true;
for each vertex w adjacent to v
if (!Mark(w)) DFS(w)
}
E.G.M. Petrakis

Graphs

33

v = v1

v1

v2
v4

v3
v5 v6

DFS :
V1V2V4V8V5V6V3V7

v7

v8
v1
v2
v3
v4
v5
v6
v7
v8
E.G.M. Petrakis

2
1
1
2
2
3
3
4

3
4
6
8
8
8
8
5

5
7

6
Graphs

7
34

DFS Algorithm
static void DFS (Graph G, int v) {
PreVisit(G, v);
// Take appropriate action
G.setMark(v, VISITED);
for ( Edge w = G.first(v); G.isEdge(w); w = G.next(w))
if (G.getMark(G.v2(w)) = = UNVISITED)
DFS ( G, G.v2(w));
PostVisit(G, v);
// Take appropriate action
}

E.G.M. Petrakis

Graphs

35

Complexity of DFS
O(|V| + |E|): adjacency list
representation
O(|V|2) in dense graphs

O(|V|2): matrix representation

E.G.M. Petrakis

Graphs

36

Breadth-First Search (BFS)


v = v1
v1
v2
v4

v3
v5

v6

FS :
V1V2V3V4V5V6V7V8

v7

v8

E.G.M. Petrakis

Graphs

37

BFS (cont.)
Starting from vertex v, all nodes unvisited, visit
adjacent nodes (use a queue)
void BFS(v) {
visited(v) = true;
enqueue(v, Q);
while ( Q 0 ) {
x = dequeue(Q);
for each y adjacent x
do if ! Mark(y) {
Mark(y) = TRUE;
enqueue(y,Q);
}
}
}

E.G.M. Petrakis

Graphs

38

v1

v = v1
v2

v3

front

v1

front

rear
v2

v3

v3

rear
v4

v5

v5

rear
v6

v7

output(v3)

rear
v8

output(v4)

front
front

v4

v4 v5 v6 v7
v8

front

v5

v6

v7

front

v6

v7

v8

front

v6

v8

E.G.M. Petrakis

output(v1)

output(v2)

rear

output(v5)
output(v6)
output(v7)

Graphs

39

Complexity of BFS
O(|V|+|E|) : adjacency list
representation
d1 + d2 + ...+ dn = |E|
di = degree (vi)
O(|V|2) : adjacency matrix
representation
E.G.M. Petrakis

Graphs

40

BFS Algorithm
void BFS (Graph G, int start) {
Queue Q(G.n( ));
Q.enqueue(start);
G.setMark(start, VISITED);
while ( !Q.isEmpty( ))
{
int v = Q.dequeue( );
PreVisit(G, v);
// Take appropriate action
for ( Edge w = G.first(v); G.isEdge(w); w = G.next(w))
if (G.getMark(G.v2(w)) = = UNVISITED) {
G.setMark(G.v2(w), VISITED); Q.enqueue(G.v2(w));
}
PostVisit(G, v);
// Take appropriate action
}
}
E.G.M. Petrakis

Graphs

41

Connected - Unconnected
B
A

C
D

connected graph: undirected


graph, there is a path
connecting any two nodes

B
C

unconnected graph:
not always a path
F
G

D
connected components

E.G.M. Petrakis

Graphs

42

Connected Components
If there exist unconnected nodes in a DFS or BFS
traversal of G then G is unconnected
Find all connected components:
void COMP(G, n)
{
for i = 1 to n
if Mark(i) == UNVISITED
then DFS(i) [or BFS(i)];
}

Complexity: O(|V| + |E|)


E.G.M. Petrakis

Graphs

43

Spanning Trees
Tree formed from edges and nodes of G
(G,E)

Spanning Tree

E.G.M. Petrakis

Graphs

44

Spanning Forest
Set of disjoint spanning trees Ti of G=(V,E)
Ti = ( Vi , Ei ) 1 i k, Vi,Ei:subsets of V, E
DFS produces depth first spanning trees
or forest
BFS breadth first spanning trees or forest
Undirected graphs provide more traversals
produce less but short spanning trees
Directed graphs provide less traversals
produce more and higher spanning trees
E.G.M. Petrakis

Graphs

45

DFS

DFS

C
D

A
B

DFS spanning tree

E.G.M. Petrakis

DFS

Graphs

DFS spanning forest

46

BFS
A

BFS spanning tree

FS

A
B

FS
C

BFS spanning
forest

E.G.M. Petrakis H

Graphs

47

Min. Cost Spanning Tree


1
6
2
3

1
5 3
6

Complexity O(|V|2)

5 4
4

Prims algorithm:

5
2

1
3

1
3

1
3

1
5 3

1
5 3

4
6

4
4

4
4

2
3

4
4

costT = 1 + 5 + 2 + 4 + 3
E.G.M. Petrakis

Graphs

48

Prims Algorithm
static void Prim(Graph G, int s, int[] D )
{//prims MST algorithm
int[] V = new int[G.n( )];
// whos closest
for (int i=0; i<G.n( ); i++) D[i] = INFINITY; // initialize
D[s] = 0;
for ( i=0; i<G.n( ); i++) {
// process the vertices
int v = minVertex(G, D);
G.setMark(v], VISITED);
if ( v != s ) AddEdgetoMST(V[v], v); // add this edge to MST
if (D[v] == INFINITY ) return;
// unreachable vertices
for ( Edge w = G.first(v); G.isEdge(w); w = G.next(w))
if (D[G.v2(w)] > G.weight(w)) {
D[G.v2(w)] = G.weight(w);
// update distance and
V[G.v2(w)] = v;
// who it came from
}
}
}
E.G.M. Petrakis

Graphs

49

Prims Algorithm (cont.)


// find min cost vertex
static int minVertex(Graph G, int[] D ) {
int v = 0;
for (int i = 0; i < G.n( ); i++)
// initialize
if (G.getMark(i) == UNVISITED) { v = i; break; }
for ( i=0; i<G.n( ); i++)
// find smallest value
if ((G.getMark(i) == UNVISITED) && ( D[i] < D[v] ))
v = i;
return v;
}

E.G.M. Petrakis

Graphs

50

Dijkstras Algorithm
Find the shortest path from a given node
to every other node in a graph G
no better algorithm for single ending node

Notation:

G = (V,E) : input graph


C[i,j] : distance between nodes i, j
V : starting node
S : set of nodes for which the shortest path
from v has been computed
D(W) : length of shortest path from v to w
passing through nodes in S

E.G.M. Petrakis

Graphs

51

10

starting point: v = 1
100

30

50

10

60

20

step

D(2)

D(3)

D(4)

D(5)

{1}

10

infinite

30

100

{1,2}

10

60

30

100

{1,2,4}

10

50

30

90

{1,2,4,3}

10

50

30

60

{1,2,4,3,5}

10

50

30

60

E.G.M. Petrakis

Graphs

52

Dijkstras Algorithm (cont.)


Dijkstra(G: graph, int v) {

S = {1};
for i = 2 to n D[i] = C[i,j];
while (S != V) {
choose w from V-S: D[w] = minimum
S = S + {w};
for each v in VS: D[v] = min{D[v], D[w]+[w,v]}*;
}
}

* If D[w]+C[w,v] < D[v] then P[v] = w: keep path in array

E.G.M. Petrakis

Graphs

53

Compute Shortest Path


static void Dijkstra(Graph G, int s, int[] D) {
for (int i=0; i < G.n( ); i++)
// initialize
D[i] = INFINITY;
D[s] = 0;
for (i = 0; i < G.n( ); i++) {
// process the vertices
int v = minVertex(G, D);
G.setMark(v, VISITED);
if (D[v] == INFINITY) return; // remaining vertices unreachable
for (Edge w = G.first(v); G.isEdge(w); w = G.next(w))
if (D[G.v2(w)] > (D[v] + G.weight(w)))
D[G.v2(w)] = D[v] + G.weight(w);
}
}

E.G.M. Petrakis

Graphs

54

Find Min. Cost Vertex


static int minVertex(Graph G, int[] D) {
int v=0;
for (int i = 0; i < G.n( ); i++)
if (G.getMark(i) == UNVISITED) { v = i; break; }
for (i++; i < G.n( ); i++)
// find smallest D value
if ((G.getMark(i) == UNVISITED) && (D[i] < D[v])) v = i;
return v;
}
minVertex: scans through the list of vertices for the min.
value O(|V|) time
Complexity: O(|V|2 + |E|) = O(|V|2) since O(|E|) is O(|V|2)

E.G.M. Petrakis

Graphs

55

Potrebbero piacerti anche