76
Data Structures and Data Structures and Algorithms Algorithms Graphs Graphs

Data Structures and Algorithms Graphs. 2Content MotivationMotivation GraphGraph –Directed Graph –Undirected Graph –Representation –Implementation AlgorithmsAlgorithms

Embed Size (px)

Citation preview

Data Structures and Algorithms Data Structures and Algorithms

GraphsGraphs

2

ContentContent

• MotivationMotivation

• GraphGraph– Directed GraphDirected Graph– Undirected GraphUndirected Graph– RepresentationRepresentation– ImplementationImplementation

• AlgorithmsAlgorithms– Graph TraversalGraph Traversal– Shortest PathShortest Path– Minimum Cost Spanning TreesMinimum Cost Spanning Trees– Critical Path AnalysisCritical Path Analysis

3

Motivation ...Motivation ...Graphs are everywhere Graphs are everywhere

4

Directed GraphDirected Graph

5

Directed GraphDirected GraphDefinitionDefinition

A A directed graphdirected graph, or , or digraphdigraph , is an ordered pair  , is an ordered pair G = (V, E) with the following properties: G = (V, E) with the following properties:

1.1. The first component, V, is a finite, non-empty set. The The first component, V, is a finite, non-empty set. The elements of V are called the elements of V are called the verticesvertices of G. of G.

2.2. The second component, E, is a finite set of ordered pairs of The second component, E, is a finite set of ordered pairs of vertices. That is, E vertices. That is, E V x V . The elements of are called the V x V . The elements of are called the edgesedges of G. of G.

ExampleExampleG = (V, E)G = (V, E)V = {a, b, c, d}V = {a, b, c, d}E = { (a,b), (a,c), (b,c), (c,a), (c,d), (d,d) }E = { (a,b), (a,c), (b,c), (c,a), (c,d), (d,d) }

RemarkRemark E cannot contain more than one instance of a given edge. E cannot contain more than one instance of a given edge. (a,b) is different then (b,a). (a,b) is different then (b,a).

Eg. Consider a downwhill (a,b) cities. Although the distance is the Eg. Consider a downwhill (a,b) cities. Although the distance is the same, the time it takes to travel a2b and b2a maybe different….same, the time it takes to travel a2b and b2a maybe different….

6

Directed Graph ... Directed Graph ...

Head, Tail, Head, Tail, AdjacentAdjacentTerminologyTerminology

Consider a directed graph G = (V, E).Consider a directed graph G = (V, E).

• Each element of V is called a Each element of V is called a vertexvertex  or a   or a nodenode    of G. Hence, V is the of G. Hence, V is the set of verticesset of vertices (or nodes) (or nodes) of G. of G.

• Each element of E is called an Each element of E is called an edgeedge  or an   or an arcarc of G. Hence, E is the of G. Hence, E is the set of edgesset of edges (or arcs) of G. (or arcs) of G.

• An edge (v,w) An edge (v,w) E can be represented as E can be represented as vvww . An arrow that points from v to w is known . An arrow that points from v to w is known as a as a directed arcdirected arc  . Vertex w is called the   . Vertex w is called the headhead of the arc because it is found at the arrow head. of the arc because it is found at the arrow head. Conversely, v is called the Conversely, v is called the tailtail of the arc. of the arc. Finally, vertex w is said to be Finally, vertex w is said to be adjacentadjacent  to   to vertex v. vertex v.

7

Directed Graph ... Directed Graph ...

Out-degree, In-degreeOut-degree, In-degreeTerminologyTerminology

Consider a directed graph G = (V, E).Consider a directed graph G = (V, E).

• An edge e=(v,w) is said to An edge e=(v,w) is said to emanateemanate from vertex v. We  from vertex v. We use notation to denote use notation to denote A(v)A(v) the set of edges emanating the set of edges emanating from vertex v. That is, A(v) = {(a,b) from vertex v. That is, A(v) = {(a,b) E : a=v} E : a=v}

• The The out-degreeout-degree of a node is the number of edges  of a node is the number of edges emanating from that node. Therefore, the out-degree emanating from that node. Therefore, the out-degree of v is |A(v)| of v is |A(v)|

• An edge e=(v,w) is said to be An edge e=(v,w) is said to be incidentincident on vertex w. We  on vertex w. We use notation use notation I(w)I(w) to denote the set of edges incident on to denote the set of edges incident on vertex w. That is, I(w) = {(a,b) vertex w. That is, I(w) = {(a,b) E : b=w} E : b=w}

• The The in-degreein-degree   of a node is the number of edges    of a node is the number of edges incident on that node. Therefore, the in-degree of w is |incident on that node. Therefore, the in-degree of w is |I(v)| I(v)|

8

Directed Graph ... Directed Graph ...

Successor, PSuccessor, PredecessorredecessorTerminologyTerminology

Consider the path P = {vConsider the path P = {v11, v, v22, ..., v, ..., vkk} in directed graph G } in directed graph G = (V, E).= (V, E).

• Vertex vVertex vi+1i+1 is the is the successorsuccessor of vertex v of vertex vii for 1 for 1 i < k. i < k. Each element vEach element vii of path P (except the last) has a of path P (except the last) has a successor. successor.

• Vertex vVertex vi-1 i-1 is the is the predecessorpredecessor of vertex v of vertex vi i for 1 < i for 1 < i k. k. Each element vEach element vii of path P (except the first) has a of path P (except the first) has a predecessor. predecessor.

ExampleExample{a, b, c, d}{a, b, c, d}

9

Directed Graph ... Directed Graph ...

PathPathDefinitionDefinition

A A pathpath in a directed graph G = (V, E) is a non-empty  in a directed graph G = (V, E) is a non-empty sequence of vertices sequence of vertices P = {vP = {v11, v, v22, ..., v, ..., vkk}}where vwhere vi i V for 1 V for 1 i i k such that (v k such that (vii, v, vi+1i+1) ) E for 1 E for 1 i i k. k. The The lengthlength of path of path PP is is kk-1. -1.

ExampleExamplePP11 = (a) = (a)PP22 = (b, c) = (b, c)PP33 = (a, b, c) = (a, b, c)PP44 = (a, c, a, c, a, c, a, c, a, c, a, c, a) = (a, c, a, c, a, c, a, c, a, c, a, c, a)

QuestionQuestionWhat is the maximum path length?What is the maximum path length?

10

Directed Graph ... Directed Graph ...

Simple path, Cycle, LoopSimple path, Cycle, LoopTerminologyTerminology

Consider the path P = {vConsider the path P = {v11, v, v22, ..., v, ..., vkk} in directed graph G = (V, E).} in directed graph G = (V, E).

• A path P is called a A path P is called a simple pathsimple path if and only if if and only if vvi i v vjj for all i and j such that for all i and j such that 1 1 i < j i < j k k. . However, it is permissible for However, it is permissible for vv1 1 = = vvkk. .

• A A cyclecycle  is a path P of non-zero length in which   is a path P of non-zero length in which vv1 1 = = vvkk. . The The lengthlength of a cycle is just the length of the path P. of a cycle is just the length of the path P.

• A A looploop  is a cycle of length one.   is a cycle of length one. That is, it is a path of the form {v, v}. That is, it is a path of the form {v, v}.

• A A simple cyclesimple cycle   is a path that is both a cycle and simple.   is a path that is both a cycle and simple.

ExampleExample{a, b, c, d}{a, b, c, d}{c, a, c, d}{c, a, c, d}{a, b, c, a}{a, b, c, a}{a, c, a, c, a} {a, c, a, c, a}

11

Directed Graph ... Directed Graph ...

Directed Acyclic GraphDirected Acyclic Graph

DefinitionDefinition A A directed, acyclic graph (DAG)directed, acyclic graph (DAG) is a directed  is a directed graph that contains no cycles. graph that contains no cycles.

RemarkRemarkTrees Trees DAG. DAG.DAG DAG Tree Tree

12

Undirected GraphUndirected Graph

13

Undirected GraphUndirected GraphDefinitionDefinition

An An undirected graphundirected graph is an ordered pair G = (V, E) with the  is an ordered pair G = (V, E) with the following properties: following properties:

1.1. The first component, V, is a finite, non-empty set. The The first component, V, is a finite, non-empty set. The elements of V are called the elements of V are called the verticesvertices of G. of G.

2.2. The second component, E, is a finite set of sets. Each The second component, E, is a finite set of sets. Each element of E is a set that is comprised of exactly two element of E is a set that is comprised of exactly two (distinct) vertices. The elements of E are called the (distinct) vertices. The elements of E are called the edgesedges of of G.G.

ExampleExampleG = ( V, E)G = ( V, E)V = {a, b, c, d}V = {a, b, c, d}E = { {a,b}, {a,c}, {b,c}, {c,d} } E = { {a,b}, {a,c}, {b,c}, {c,d} }

RemarkRemark

• {a,b} = {b,a} {a,b} = {b,a} undirected *eg. full duplex communication undirected *eg. full duplex communication link between nodeslink between nodes

• There cannot be an edge from a node to itself in an There cannot be an edge from a node to itself in an undirected graph undirected graph

14

Undirected Graph ... Undirected Graph ...

IncidentIncidentTerminologyTerminology

Consider a undirected graph G = (V, E).Consider a undirected graph G = (V, E).

• An edge e={v,w} An edge e={v,w} E is said to E is said to emanate fromemanate from and and incident onincident on both vertices v and w. both vertices v and w.

• The set of edges emanating from a vertex v is The set of edges emanating from a vertex v is the set the set A(v)A(v) = {(v = {(v00,v,v11) ) E : v E : v00 = v = v v v11 = v} = v}

• The set of edges incident on a vertex w is The set of edges incident on a vertex w is I(w)I(w) = A(w) = A(w)

15

LabeledLabeled GraphGraph

DefinitionDefinition A graph which has been annotated in some A graph which has been annotated in some way is called a way is called a labeledlabeled graphgraph..

RemarkRemarkBoth edges and vertices can be labeledBoth edges and vertices can be labeled

16

RepresentationRepresentation

17

RepresentationRepresentation

Consider a directed graph G = (V, E). Consider a directed graph G = (V, E).

• Since E Since E V V V, graph G contains at V, graph G contains at most |V|most |V|22 edges. edges.

• There are possible sets of There are possible sets of edges for a given set of vertices .edges for a given set of vertices .

2

2V

18

Representation ...Representation ... Adjacency Matrix for DAGAdjacency Matrix for DAG

Consider a directed graph G = (V, E) Consider a directed graph G = (V, E) with V = {vwith V = {v11, v, v22, ..., v, ..., vnn}. }.

• The simplest graph representation The simplest graph representation scheme uses an n x n matrix A of scheme uses an n x n matrix A of zeroes and ones given by zeroes and ones given by

• The matrix A is called an The matrix A is called an adjacency adjacency matrixmatrix. .

19

Representation ...Representation ... Adjacency Matrix for DAG ...Adjacency Matrix for DAG ...

ExampleExample

RemarkRemark

• The number of ones in the adjacency matrix is The number of ones in the adjacency matrix is equal to the number of edges in the graphequal to the number of edges in the graph

• Each one in the iEach one in the ithth row corresponds to an edge row corresponds to an edge that emanates from vertex vthat emanates from vertex v ii. .

• Each one in the iEach one in the ithth column corresponds to an edge column corresponds to an edge incident on vertex vincident on vertex vii. .

20

Representation ...Representation ... Adjacency Matrix for Adjacency Matrix for undirectedundirectedRepresent an undirected graph G = Represent an undirected graph G =

(V, E) with (V, E) with V = {vV = {v11, v, v22, ..., v, ..., vnn}, }, using an n x n matrix using an n x n matrix AA of zeroes of zeroes and ones given by and ones given by

21

Representation ...Representation ... Adjacency Matrix for undirected ...Adjacency Matrix for undirected ...

ExampleExample

RemarkRemark

• Since {vSince {vii, v, vjj} = {v} = {vjj, v, vii}, matrix }, matrix AA is symmetric is symmetric about the diagonal. about the diagonal. That is, aThat is, aijij = a = ajiji

• All of the entries on the diagonal are zero. All of the entries on the diagonal are zero. That is, aThat is, aiiii = 0 for 1 = 0 for 1 i i n n

22

Representation ...Representation ... Adjacency Matrix for undirected ...Adjacency Matrix for undirected ...

ExampleExample

RemarkRemark

• Since {vSince {vii, v, vjj} = {v} = {vjj, v, vii}, matrix }, matrix AA is symmetric is symmetric about the diagonal. about the diagonal. That is, aThat is, aijij = a = ajiji

• All of the entries on the diagonal are zero. All of the entries on the diagonal are zero. That is, aThat is, aiiii = 0 for 1 = 0 for 1 i i n n

23

Representation > Adjacency Matrix ... Representation > Adjacency Matrix ...

ComplexityComplexity

• Since the adjacency matrix has |V|Since the adjacency matrix has |V|22 entries, entries, the amount of spaced needed to the amount of spaced needed to represent the edges of a graph is represent the edges of a graph is O( |V|O( |V|22 ) ) regardless of the actual number of edgesregardless of the actual number of edges in the graph.in the graph.

• If |E| << |V|If |E| << |V|22 then most of the entries are then most of the entries are 00

• Wasteful representation !Wasteful representation !

24

Representation > Adjacency Matrix ... Representation > Adjacency Matrix ...

Sparse and Dense Graphs Sparse and Dense Graphs

DefinitionDefinitionA A sparse graphsparse graph is a graph G = (V, E)  is a graph G = (V, E) in which |E| = O( |V| )in which |E| = O( |V| )

DefinitionDefinitionA A dense graphdense graph is a graph G = (V, E)  is a graph G = (V, E) in which |E| = in which |E| = ( |V|( |V|22 ) )

25

Representation > Adjacency Matrix ... Representation > Adjacency Matrix ...

Adjacency ListsAdjacency Lists

26

RepresentationRepresentation

Comparison of Comparison of RepresentationsRepresentations

operation adjacency matrix

adjacency list

find edge (v,w) O(1) O( |A(v)| )

enumerate all edges

O( |V|2 ) O( |V| + |E| )

enumerate edges emanating from v

O( |V| ) O( |A(v)| )

enumerate edges incident on w

O( |V| ) O( |V| + |E| )

27

Implementation(*)Implementation(*)

28

Implementation (*)Implementation (*)VertexVertex

import java.util.Enumeration;

public interface Vertex extends Comparable { int getNumber(); Object getWeight(); Enumeration getIncidentEdges(); Enumeration getEmanatingEdges(); Enumeration getPredecessors(); Enumeration getSuccessors();}

29

Implementation (*)Implementation (*)EdgeEdge

public interface Edge extends Comparable {

Vertex getV0(); Vertex getV1(); Object getWeight(); boolean isDirected(); Vertex getMate(Vertex vertex);}

30

Implementation (*)Implementation (*)GraphGraph

import java.util.Enumeration;

public interface Graph extends Container { int getNumberOfEdges(); int getNumberOfVertices(); boolean isDirected(); void addVertex(int v); void addVertex(int v, Object weight); Vertex getVertex(int v); void addEdge(int v, int w); void addEdge(int v, int w, Object weight); Edge getEdge(int v, int w); boolean isEdge(int v, int w); boolean isConnected(); boolean isCyclic(); Enumeration getVertices(); Enumeration getEdges(); void depthFirstTraversal(PrePostVisitor visitor, int start); void breadthFirstTraversal(Visitor visitor, int start);}

31

Implementation (*)Implementation (*)Directed GraphDirected Graph

public interface Digraph extends Graph { boolean isStronglyConnected(); void topologicalOrderTraversal(Visitor visitor);}

32

Graph TraversalGraph Traversal

33

Graph Traversal Graph Traversal Depth-First TraversalDepth-First Traversal

• similar to depth-first tree traversalsimilar to depth-first tree traversal

• because there is not root, because there is not root, we must specify a starting nodewe must specify a starting node

• because a graph may be cyclic, because a graph may be cyclic, we must prevent infinite recursionwe must prevent infinite recursion

34

Graph Traversal Graph Traversal Depth-First Traversal ...Depth-First Traversal ...

The depth-first traversal visits the nodes in the order The depth-first traversal visits the nodes in the order c, a, b, dc, a, b, d

RemarkRemark

• A depth-first traversal only follows edges that lead to unvisited vertices. A depth-first traversal only follows edges that lead to unvisited vertices.

• if we omit the edges that are not followed, the remaining edges form a if we omit the edges that are not followed, the remaining edges form a tree. tree.

• The depth-first traversal of this tree is equivalent to the depth-first The depth-first traversal of this tree is equivalent to the depth-first traversal of the graph traversal of the graph

35

Graph Traversal > Depth-First Graph Traversal > Depth-First Traversal ...Traversal ...Implementation (*)Implementation (*)

public abstract class AbstractGraph extends AbstractContainer implements Graph { ... public void depthFirstTraversal(PrePostVisitor prepostvisitor, int i) { boolean aflag[] = new boolean[numberOfVertices]; for (int j = 0; j < numberOfVertices; j++) aflag[j] = false;

depthFirstTraversal(prepostvisitor, vertex[i], aflag); }

private void depthFirstTraversal( PrePostVisitor prepostvisitor, Vertex vertex1, boolean aflag[]) { if (prepostvisitor.isDone()) return; prepostvisitor.preVisit(vertex1); aflag[vertex1.getNumber()] = true; for (Enumeration enumeration = vertex1.getSuccessors(); enumeration.hasMoreElements(); ) { Vertex vertex2 = (Vertex) enumeration.nextElement(); if (!aflag[vertex2.getNumber()]) depthFirstTraversal(prepostvisitor, vertex2, aflag); }

prepostvisitor.postVisit(vertex1); }}

36

Graph Traversal Graph Traversal Breadth-First TraversalBreadth-First Traversal

• similar to breadth-first tree traversalsimilar to breadth-first tree traversal

• uses a queue of vertices uses a queue of vertices

• because there is not root, because there is not root, we must specify a starting nodewe must specify a starting node

• because a graph may be cyclic, because a graph may be cyclic, we must ensure that a vertex is we must ensure that a vertex is enqueued only onceenqueued only once

37

Graph Traversal Graph Traversal Breadth-First Traversal ...Breadth-First Traversal ...

The breadth-first traversal visits the nodes in the The breadth-first traversal visits the nodes in the order order a, b, c, da, b, c, d

38

Graph Traversal > Breadth-First Graph Traversal > Breadth-First Traversal ...Traversal ...Implementation(*)Implementation(*)public abstract class AbstractGraph extends AbstractContainer implements Graph { ... public void breadthFirstTraversal(Visitor visitor, int i) { boolean aflag[] = new boolean[numberOfVertices]; for (int j = 0; j < numberOfVertices; j++) aflag[j] = false;

QueueAsLinkedList queueaslinkedlist = new QueueAsLinkedList(); aflag[i] = true; queueaslinkedlist.enqueue(vertex[i]); while (!queueaslinkedlist.isEmpty() && !visitor.isDone()) { Vertex vertex1 = (Vertex) queueaslinkedlist.dequeue(); visitor.visit(vertex1); for (Enumeration enumeration = vertex1.getSuccessors(); enumeration.hasMoreElements(); ) { Vertex vertex2 = (Vertex) enumeration.nextElement(); if (!aflag[vertex2.getNumber()]) { aflag[vertex2.getNumber()] = true; queueaslinkedlist.enqueue(vertex2); } }

} } ...}

39

Graph TraversalGraph TraversalTopological SortTopological Sort

• DefinitionDefinitionConsider a DAG with G = (V, E).Consider a DAG with G = (V, E).

A A topological sorttopological sort of the vertices of G is a of the vertices of G is a sequencesequenceS = { vS = { v11, v, v22, ..., v, ..., v|V||V| } in which each element of V } in which each element of V appears exactly once.appears exactly once.

For every pair of distinct vertices vFor every pair of distinct vertices v ii and v and vjj in the in the sequence S, if vsequence S, if vii v vjj is an edge in G, i.e., (v is an edge in G, i.e., (vi i ,, vvjj) ) E, then i < j. E, then i < j.

40

Graph TraversalGraph TraversalTopological Order Topological Order TraversalTraversal

• a traversal that visits the a traversal that visits the vertices of a DAG in the vertices of a DAG in the order given by the order given by the topological sorttopological sort

• compute the in-degrees of compute the in-degrees of the verticesthe vertices

• repeatedly select a vertex repeatedly select a vertex with zero in-degree, visit with zero in-degree, visit it, and then “remove it it, and then “remove it from the graph”from the graph”

SS11 = { a, b, c, d, e, f, g, h, i } = { a, b, c, d, e, f, g, h, i }

SS22 = { a, c, b, f, e, d, h, g, i } = { a, c, b, f, e, d, h, g, i }

SS33 = { a, b, d, e, g, c, f, h, i } = { a, b, d, e, g, c, f, h, i }

......

There are manyThere are many

41

• Is it connected Is it connected

• Is there any cycles Is there any cycles

Graph TraversalGraph TraversalApplicationsApplications

1 01 0

22

3 3

9 9

44

88

7 7

5 5

66

7 7

42

Graph Traversal > ApplicationsGraph Traversal > ApplicationsConnectednessConnectedness

DefinitionDefinition An undirected graph G = (V, E) is An undirected graph G = (V, E) is connectedconnected      if there is a path in G between every pair if there is a path in G between every pair of vertices in V.of vertices in V.

The connected sub-graphs of a graph are The connected sub-graphs of a graph are called called connected componentsconnected components..

43

Graph Traversal > ApplicationsGraph Traversal > ApplicationsConnectednessConnectedness

DefinitionDefinition A directed graph G = (V, E) is A directed graph G = (V, E) is strongly connected strongly connected if there is a if there is a path in G between every pair path in G between every pair of vertices in V.of vertices in V.

DefinitionDefinition A directed graph G = (V, E) is A directed graph G = (V, E) is weakly connected weakly connected if the if the underlying undirected graph Ğ underlying undirected graph Ğ is connected. is connected.

44

Graph Traversal > ApplicationsGraph Traversal > ApplicationsConnectednessConnectedness

AlgorithmAlgorithm

For all verticesFor all vertices

Start from each vertexStart from each vertex

Traverse the graphTraverse the graph

Mark every visited vertexMark every visited vertex

If there is an unvisited If there is an unvisited vertex, vertex, unconnectedunconnected

45

Graph Traversal > ApplicationsGraph Traversal > ApplicationsCycleCycle

AlgorithmAlgorithm

Apply topological-order Apply topological-order traversaltraversal

46

Shortest Path Shortest Path

HW:HW:Find the shortest-path from your Find the shortest-path from your hometown to any other city in Turkiyehometown to any other city in Turkiyeemploy at least three different employ at least three different techniques, and find the run time techniques, and find the run time complexity using complexity using http://java.com/en/download/index.jsp

Use the excel chart below for city-Use the excel chart below for city-distancesdistanceshttp://www.kgm.gov.tr/

•http://www.unf.edu/~wkloster/foundations/DijkstraApplet/DijkstraApplet.htm

47

Shortest PathShortest PathDistanceDistance

• Distance between two vertices Distance between two vertices – 2-92-9

– 3-93-9

– 7-47-41 01 0

22

3 3

9 9

44

88

7 7

5 5

66

7 7

48

Shortest PathShortest PathWeighted Path LengthWeighted Path Length

DefinitionDefinition Consider an edge-weighted graph G = Consider an edge-weighted graph G = (V, E) . Let C(v(V, E) . Let C(vii,v,vjj) be the weight on the ) be the weight on the edge connecting vedge connecting vii to v to vjj. A path in G is a . A path in G is a non-empty sequence of vertices P = {vnon-empty sequence of vertices P = {v11, , vv22, ..., v, ..., vkk} . The } . The weighted path lengthweighted path length of  of path P is given by path P is given by

1

11),(

k

iii vvc

49

Shortest PathShortest PathSingle-source shortest Single-source shortest path path

DefinitionDefinition Single-source shortest pathSingle-source shortest path problem problem Given an edge-weighted graph G = (V, E) and a Given an edge-weighted graph G = (V, E) and a vertex vvertex vs s V V, find the shortest weighted path from v, find the shortest weighted path from vss to every other vertex in V. to every other vertex in V.

RemarkRemarkIf all weights are positive, it is well-defined. If all weights are positive, it is well-defined.

50

• if the graph is acyclic, the problem if the graph is acyclic, the problem is easyis easy– do a topological order traversaldo a topological order traversal

• as long as all the edge weights are as long as all the edge weights are non-negative the shortest-path non-negative the shortest-path problem is well definedproblem is well defined– a greedy algorithm works (Dijkstra's a greedy algorithm works (Dijkstra's

algorithm)algorithm)

Shortest PathShortest PathSpecial Cases Special Cases non-negative weightsnon-negative weights

51

• if the graph has negative weights if the graph has negative weights (but not negative cost cycles) a (but not negative cost cycles) a solution exists but the greedy solution exists but the greedy algorithm does not workalgorithm does not work

• if the graph has a negative cost if the graph has a negative cost cycle, cycle, no solution existsno solution exists

Shortest PathShortest PathSpecial Cases Special Cases negative weightsnegative weights

52

Shortest PathShortest PathDijkstra’s AlgorithmDijkstra’s Algorithm

kkvv indicates that the shortest path to vertex v is indicates that the shortest path to vertex v is known. Initially falseknown. Initially false

ddvv the length of the shortest known path from v the length of the shortest known path from vss to to v. Initially v. Initially ..

ppvv The predecessor of vertex v on the shortest path The predecessor of vertex v on the shortest path from vfrom vss to v. Initially unknown. to v. Initially unknown.

53

Shortest PathShortest PathDijkstra’s AlgorithmDijkstra’s Algorithm

1.1. From the set of vertices for with kFrom the set of vertices for with kvv=false, select the =false, select the vertex v having the smallest tentative distance dvertex v having the smallest tentative distance dvv. .

2.2. Set kSet kvv=true =true

3.3. For each vertex w adjacent to v for which kFor each vertex w adjacent to v for which kvvtrue, true, test whether the tentative distance dtest whether the tentative distance dvv is greater than is greater than ddvv+c(v,w). If it is, set d+c(v,w). If it is, set dv v d dvv+c(v,w) and set p+c(v,w) and set pv v v. v.

In each pass exactly one vertex has its kIn each pass exactly one vertex has its kvv set to true. The set to true. The algorithm terminates after |V|passes are completed algorithm terminates after |V|passes are completed at which time all the shortest paths are known. at which time all the shortest paths are known.

54

Shortest Path Shortest Path ::Dijkstra’s AlgorithmDijkstra’s Algorithm

•http://www.lupinho.de/gishur/html/DijkstraApplet.html

55

Shortest PathShortest PathAll-pairs source shortest All-pairs source shortest path path

DefinitionDefinition All-pairs source shortest pathAll-pairs source shortest path problem problem

Given an edge-weighted graph G = (V, Given an edge-weighted graph G = (V, E), for each pair of vertices in V find the E), for each pair of vertices in V find the length of the shortest weighted path length of the shortest weighted path between the two vertices.between the two vertices.

56

Shortest Path with Floyd’s AlgorithmShortest Path with Floyd’s Algorithm

The idea: Determine whether a path going from The idea: Determine whether a path going from Vi to Vj via Vk is shorter than the best-known Vi to Vj via Vk is shorter than the best-known path from Vi to Vk .path from Vi to Vk .  

VVkk the first k vertices in V the first k vertices in V

PPkk(v,w) the shortest path from vertex v to w that passes (v,w) the shortest path from vertex v to w that passes only through vertices in Vonly through vertices in Vkk

DDkk(v,w) the length of path P(v,w) the length of path Pkk(v;w)(v;w)

57

Shortest PathShortest Path: : Floyd’s AlgorithmFloyd’s AlgorithmDD00(v,w) is the adjacency matrix(v,w) is the adjacency matrix

compute the sequence of matrices Dcompute the sequence of matrices D00, D, D11, ..., D, ..., D|V| |V|

obtain the distances in Dobtain the distances in Di+1i+1 from those in D from those in Dii by by considering only the paths that pass through considering only the paths that pass through vertex vvertex vi+1i+1::

DDi+1i+1(v;w) = min{ D(v;w) = min{ Dii(v, v(v, vi+1i+1)+D)+Dii(v(vi+1i+1, w);D, w);Dii(v,w)}(v,w)}

HWLA1: Search, Floyds algorithm with O (V^3)HWLA1: Search, Floyds algorithm with O (V^3)HWLA2: Read, http://www-unix.mcs.anl.gov/dbpp/text/node35.html

58

Minimum Cost Spanning Minimum Cost Spanning Trees Trees

59

Minimum Cost Spanning TreesMinimum Cost Spanning TreesSSpanning Tree panning Tree

DefinitionDefinitionConsider a connected, undirected graph G=(V, E).Consider a connected, undirected graph G=(V, E).A A spanning treespanning tree T = (V’, E’) of G is a subgraph of G T = (V’, E’) of G is a subgraph of G with the following properties:with the following properties:

1.1. V’ = VV’ = V

2.2. T is connectedT is connected

3.3. T is acyclicT is acyclic

RemarkRemarkSpanning tree is a tree.Spanning tree is a tree.

60

Minimum Cost Spanning TreesMinimum Cost Spanning TreesTotal CostTotal Cost

DefinitionDefinitionThe The total cost total cost of an edge-weighted of an edge-weighted undirected graph is the sum of the undirected graph is the sum of the weights on all the edges in that weights on all the edges in that graphgraph

61

Minimum Cost Spanning TreesMinimum Cost Spanning TreesMMinimum Spanning Treeinimum Spanning Tree

DefinitionDefinition Consider an edge-weighted, undirected, Consider an edge-weighted, undirected, connected graph G=(V, E), where c(v, w) connected graph G=(V, E), where c(v, w) represents the weight on edge {v, w}represents the weight on edge {v, w}E. E. The The minimum spanning treeminimum spanning tree of G is the  of G is the spanning tree T=(V, E’) that has the spanning tree T=(V, E’) that has the smallest total cost, smallest total cost,

(a simple idea: remove the edges with large weights without loosing (a simple idea: remove the edges with large weights without loosing connection)connection)

62

Minimum Cost Spanning Trees:Minimum Cost Spanning Trees: PrimPrim

• is a minor variation of Dijkstra's algorithm for shortest path

• constructs the minimum-cost spanning tree of a graph by selecting edges from the graph one-by-one and adding those edges to the spanning tree

• at each step, select an edge with the smallest edge weight that connects the tree to a vertex not yet in the tree

63

Minimum Cost Spanning TreesMinimum Cost Spanning TreesPrim’s AlgorithmPrim’s Algorithm

64

Minimum Cost Spanning Tree: Minimum Cost Spanning Tree: KruskalKruskal• is another greedy based approach that

continually selects the edges in order of smallest weight without causing a cycle.

• Simple idea: Kruskal maintans a forest-a collection of trees.

– Select the smallest weights first to establish forests (set of smaller trees)

– Merge them at the end.

• Worst case running time: O(|E| Log|E|) which is dominated by heap operations. Note: since |E|=|V|^2, O(|E| Log|V|)

• Ex: Solve Figure 9.48 using Kruskal’s algorithmEx: Solve Figure 9.48 using Kruskal’s algorithm

•http://students.ceid.upatras.gr/~papagel/minKrusk.htm

•http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/main/index.shtml

65

Critical Path Analysis(*) Critical Path Analysis(*)

•http://www.mindtools.com/critpath.html

66

•66

Eg. Satellite Systems Eg. Satellite Systems Engineering Engineering

•Communication Network

Architectural Candidate Options

•Orbit: LEO/MEO/GEO

•Modulation/Coding

•Payload: Processing/ Non-Processing

•Antenna: Global/Spot beam

•Multiple Access: FDMA/TDMA/CDMA DAMA

•Switching: Circuit/ATM/Packet

•IntServ/DiffServ/MPLS

•Frequency Band: Ku/Ka/Q/V

•Transport: TCP/UDP

•Applications

•QoS Requirements

•Guaranteed Bandwidth

•BER

•CLR, CER, CDV

•IPLR, IPER, IPDV, IPTD

•End-to-End Delay

•Delay Variation

•Reliability

•…

•Systems Engineering Analysis

•Content Distribution

•Broadband Access

•Streaming Video

•Internet Applications

•Distributions

•Global

•Regional

•Operational Concept System Performance System Implementation

•Baseline Architectur

e

•Output Products

•Evaluation/ Analysis

•Preferred Network Design

•Traffic Require-ments

•Applications

•Business Plan

•System Cost /

Implement. Complexity

Output Products

•Trends Analysis of SatComNets

•End-to-end QoS

•TCP over S.ATM

•S.IP – Security, Multicast

•Satellite ATM QoS

•Buffer Requirements

•TCP Analysis

•Bandwidth Allocation

•Satellite IP QoS

•QoS Architecture

•TCP/UDP with DiffServ

•MPLS over Satellite

•Satellite Internet Access

•MF-TDMA vs. DOCSIS

•Spread ALOHA-Return Channel

67

Critical Path Analysis(*) Critical Path Analysis(*)

•http://www.mindtools.com/critpath.html

68

Critical Path AnalysisCritical Path AnalysisActivity-Node GraphsActivity-Node Graphs

• represent a set of activities and represent a set of activities and scheduling constraints using a graphscheduling constraints using a graph

• each vertex represents an activityeach vertex represents an activity

• the weight on the vertex represents the the weight on the vertex represents the time required to complete the activitytime required to complete the activity

• directed edges represent the sequencing directed edges represent the sequencing constraintsconstraints

• the graph must be acyclicthe graph must be acyclic

69

Critical Path AnalysisCritical Path AnalysisCritical path analysis Critical path analysis QuestionsQuestions

• What is the minimum amount of What is the minimum amount of time needed to complete all time needed to complete all activities?activities?

• For a given activity v, is it possible For a given activity v, is it possible to delay the completion of that to delay the completion of that activity without affecting the overall activity without affecting the overall completion time?completion time?

• If yes, by how much can the If yes, by how much can the completion of activity v be delayed?completion of activity v be delayed?

70

Critical Path AnalysisCritical Path AnalysisEvent-Node GraphsEvent-Node Graphs

• edges represent the activitiesedges represent the activities

• weights on edges represent the weights on edges represent the time required for the activitiestime required for the activities

• vertices represent the vertices represent the commencement and termination of commencement and termination of activities i.e., eventsactivities i.e., events

71

Critical Path AnalysisCritical Path AnalysisEarliest and Latest Event Earliest and Latest Event TimesTimesEEvv the earliest time at which event v the earliest time at which event v

can occurcan occur

LLvv the latest time at which event v can the latest time at which event v can occuroccur

72

Critical Path AnalysisCritical Path AnalysisSlack TimeSlack Time

• consider an activity represented by edge consider an activity represented by edge (v, w)(v, w)

• the amount of time available for the the amount of time available for the activity is Lactivity is Lww - E - Evv

• the time required for that activity is c(v, w)the time required for that activity is c(v, w)

• the the slack timeslack time for activity (v, w) is for activity (v, w) is S(v,w) = L S(v,w) = Lww - E - Evv - c(v, w) - c(v, w)

73

Critical Path AnalysisCritical Path AnalysisCritical Activity and PathCritical Activity and Path

DefinitionDefinitionAn activity with zero slack is An activity with zero slack is criticalcritical

DefinitionDefinition A A critical pathcritical path is a path from the is a path from the initial vertex to the final vertex initial vertex to the final vertex comprised solely of critical activitiescomprised solely of critical activities

74

Critical Path AnalysisCritical Path AnalysisExampleExample

75

SummarySummary

• GraphGraph– Directed GraphDirected Graph– Undirected GraphUndirected Graph– RepresentationRepresentation– ImplementationImplementation

• Graph TraversalGraph Traversal

• Shortest PathShortest Path

• Minimum Cost Spanning TreesMinimum Cost Spanning Trees

• Critical Path AnalysisCritical Path Analysis

HW: 1, 5, 6, 7, 10, 11, 15, 16, 20, 21, 30HW: 1, 5, 6, 7, 10, 11, 15, 16, 20, 21, 30

76

ReferencesReferences

• Introductory Graph TheoryIntroductory Graph TheoryChartrandChartrand Dover, 1977Dover, 1977

• Data Structures and Algorithms Data Structures and Algorithms with Object-Oriented Design Patterns in Javawith Object-Oriented Design Patterns in JavaPreiss Preiss http://www.brpreiss.com/books/opus5/http://www.brpreiss.com/books/opus5/

• Data Structures and AlgorithmsData Structures and Algorithmswith Object-Oriented Design Patters in C++with Object-Oriented Design Patters in C++PreissPreissWiley, 1999 Wiley, 1999

• Data Structures and Algorithm Analysis in JavaData Structures and Algorithm Analysis in JavaWeissWeissAddison-Wesley, 1999Addison-Wesley, 1999