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

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Data Structures Using C++
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Testing for Connectedness and Cycles
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Chapter 9: Graphs Basic Concepts
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Social Media Mining Graph Essentials.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Data Structures Using C++ 2E
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Data Structures and Algorithms Graphs Fall 2006 Major part of this presentation is courtesy of Dr.Bingol.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Chapter 9: Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Data Structures 13th Week
Introduction to Graphs
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Chapter 9: Graphs Basic Concepts
Chapter 11 Graphs.
Chapter 9: Graphs Basic Concepts
Chapter 9 Graph algorithms
Introduction to Graphs
Introduction to Graphs
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Data Structures and Algorithms Graphs

2Content MotivationMotivation GraphGraph –Directed Graph –Undirected Graph –Representation –Implementation AlgorithmsAlgorithms –Graph Traversal –Shortest Path –Minimum Cost Spanning Trees –Critical Path Analysis

3 Motivation... Graphs are everywhere

4 Directed Graph

5 Definition A directed graph, or digraph, is an ordered pair G = (V, E) with the following properties: 1.The first component, V, is a finite, non-empty set. The elements of V are called the vertices of G. 2.The second component, E, is a finite set of ordered pairs of vertices. That is, E  V x V. The elements of are called the edges of G. Example G = (V, E) V = {a, b, c, d} E = { (a,b), (a,c), (b,c), (c,a), (c,d), (d,d) } Remark E cannot contain more than one instance of a given edge. (a,b) is different then (b,a). Eg. Consider a downwhill (a,b) cities. Although the distance is the same, the time it takes to travel a2b and b2a maybe different…. Eg. Consider a downwhill (a,b) cities. Although the distance is the same, the time it takes to travel a2b and b2a maybe different….

6 Directed Graph... Head, Tail, Adjacent Terminology Consider a directed graph G = (V, E). Each element of V is called a vertex or a node of G. Hence, V is the set of vertices (or nodes) of G.Each element of V is called a vertex or a node of G. Hence, V is the set of vertices (or nodes) of G. Each element of E is called an edge or an arc of G. Hence, E is the set of edges (or arcs) of G.Each element of E is called an edge or an arc of G. Hence, E is the set of edges (or arcs) of G. An edge (v,w)  E can be represented as v  w. An arrow that points from v to w is known as a directed arc. Vertex w is called the head of the arc because it is found at the arrow head. Conversely, v is called the tail of the arc. Finally, vertex w is said to be adjacent to vertex v.An edge (v,w)  E can be represented as v  w. An arrow that points from v to w is known as a directed arc. Vertex w is called the head of the arc because it is found at the arrow head. Conversely, v is called the tail of the arc. Finally, vertex w is said to be adjacent to vertex v.

7 Directed Graph... Out-degree, In-degree Terminology Consider a directed graph G = (V, E). An edge e=(v,w) is said to emanate from vertex v. We use notation to denote A(v) the set of edges emanating from vertex v. That is, A(v) = {(a,b)  E : a=v}An edge e=(v,w) is said to emanate from vertex v. We use notation to denote A(v) the set of edges emanating from vertex v. That is, A(v) = {(a,b)  E : a=v} The out-degree of a node is the number of edges emanating from that node. Therefore, the out-degree of v is |A(v)|The out-degree of a node is the number of edges emanating from that node. Therefore, the out-degree of v is |A(v)| An edge e=(v,w) is said to be incident on vertex w. We use notation I(w) to denote the set of edges incident on vertex w. That is, I(w) = {(a,b)  E : b=w}An edge e=(v,w) is said to be incident on vertex w. We use notation I(w) to denote the set of edges incident on vertex w. That is, I(w) = {(a,b)  E : b=w} The in-degree of a node is the number of edges incident on that node. Therefore, the in-degree of w is |I(v)|The in-degree of a node is the number of edges incident on that node. Therefore, the in-degree of w is |I(v)|

8 Directed Graph... Successor, P redecessor Terminology Consider the path P = {v 1, v 2,..., v k } in directed graph G = (V, E). Vertex v i+1 is the successor of vertex v i for 1  i < k. Each element v i of path P (except the last) has a successor.Vertex v i+1 is the successor of vertex v i for 1  i < k. Each element v i of path P (except the last) has a successor. Vertex v i-1 is the predecessor of vertex v i for 1 < i  k. Each element v i of path P (except the first) has a predecessor.Vertex v i-1 is the predecessor of vertex v i for 1 < i  k. Each element v i of path P (except the first) has a predecessor. Example {a, b, c, d}

9 Directed Graph... Path Definition A path in a directed graph G = (V, E) is a non-empty sequence of vertices P = {v 1, v 2,..., v k } where v i  V for 1  i  k such that (v i, v i+1 )  E for 1  i  k. The length of path P is k-1. Example P 1 = (a) P 2 = (b, c) P 3 = (a, b, c) P 4 = (a, c, a, c, a, c, a, c, a, c, a, c, a) Question What is the maximum path length?

10 Directed Graph... Simple path, Cycle, Loop Terminology Consider the path P = {v 1, v 2,..., v k } in directed graph G = (V, E). A path P is called a simple path if and only if v i  v j for all i and j such that 1  i < j  k. However, it is permissible for v 1 = v k.A path P is called a simple path if and only if v i  v j for all i and j such that 1  i < j  k. However, it is permissible for v 1 = v k. A cycle is a path P of non-zero length in which v 1 = v k. The length of a cycle is just the length of the path P.A cycle is a path P of non-zero length in which v 1 = v k. The length of a cycle is just the length of the path P. A loop is a cycle of length one. That is, it is a path of the form {v, v}.A loop is a cycle of length one. That is, it is a path of the form {v, v}. A simple cycle is a path that is both a cycle and simple.A simple cycle is a path that is both a cycle and simple. Example {a, b, c, d} {c, a, c, d} {a, b, c, a} {a, c, a, c, a}

11 Directed Graph... Directed Acyclic Graph Definition A directed, acyclic graph (DAG) is a directed graph that contains no cycles. Remark Trees  DAG. DAG  Tree

12 Undirected Graph

13 Undirected Graph Definition An undirected graph is an ordered pair G = (V, E) with the following properties: 1.The first component, V, is a finite, non-empty set. The elements of V are called the vertices of G. 2.The second component, E, is a finite set of sets. Each element of E is a set that is comprised of exactly two (distinct) vertices. The elements of E are called the edges of G. Example G = ( V, E) V = {a, b, c, d} E = { {a,b}, {a,c}, {b,c}, {c,d} } Remark {a,b} = {b,a}  undirected *eg. full duplex communication link between nodes{a,b} = {b,a}  undirected *eg. full duplex communication link between nodes There cannot be an edge from a node to itself in an undirected graphThere cannot be an edge from a node to itself in an undirected graph

14 Undirected Graph... Incident Terminology Consider a undirected graph G = (V, E). An edge e={v,w}  E is said to emanate from and incident on both vertices v and w.An edge e={v,w}  E is said to emanate from and incident on both vertices v and w. The set of edges emanating from a vertex v is the set A(v) = {(v 0,v 1 )  E : v 0 = v  v 1 = v}The set of edges emanating from a vertex v is the set A(v) = {(v 0,v 1 )  E : v 0 = v  v 1 = v} The set of edges incident on a vertex w is I(w) = A(w)The set of edges incident on a vertex w is I(w) = A(w)

15 Labeled Graph Definition A graph which has been annotated in some way is called a labeled graph. Remark Both edges and vertices can be labeled

16 Representation

17Representation Consider a directed graph G = (V, E). Since E  V  V, graph G contains at most |V| 2 edges.Since E  V  V, graph G contains at most |V| 2 edges. There are possible sets of edges for a given set of vertices.There are possible sets of edges for a given set of vertices.

18 Representation... Adjacency Matrix for DAG Consider a directed graph G = (V, E) with V = {v 1, v 2,..., v n }. The simplest graph representation scheme uses an n x n matrix A of zeroes and ones given byThe simplest graph representation scheme uses an n x n matrix A of zeroes and ones given by The matrix A is called an adjacency matrix.The matrix A is called an adjacency matrix.

19 Representation... Adjacency Matrix for DAG... ExampleRemark The number of ones in the adjacency matrix is equal to the number of edges in the graphThe number of ones in the adjacency matrix is equal to the number of edges in the graph Each one in the i th row corresponds to an edge that emanates from vertex v i.Each one in the i th row corresponds to an edge that emanates from vertex v i. Each one in the i th column corresponds to an edge incident on vertex v i.Each one in the i th column corresponds to an edge incident on vertex v i.

20 Representation... Adjacency Matrix for undirected Represent an undirected graph G = (V, E) with V = {v 1, v 2,..., v n }, using an n x n matrix A of zeroes and ones given by

21 Representation... Adjacency Matrix for undirected... ExampleRemark Since {v i, v j } = {v j, v i }, matrix A is symmetric about the diagonal. That is, a ij = a jiSince {v i, v j } = {v j, v i }, matrix A is symmetric about the diagonal. That is, a ij = a ji All of the entries on the diagonal are zero. That is, a ii = 0 for 1  i  nAll of the entries on the diagonal are zero. That is, a ii = 0 for 1  i  n

22 Representation... Adjacency Matrix for undirected... ExampleRemark Since {v i, v j } = {v j, v i }, matrix A is symmetric about the diagonal. That is, a ij = a jiSince {v i, v j } = {v j, v i }, matrix A is symmetric about the diagonal. That is, a ij = a ji All of the entries on the diagonal are zero. That is, a ii = 0 for 1  i  nAll of the entries on the diagonal are zero. That is, a ii = 0 for 1  i  n

23 Representation > Adjacency Matrix... Complexity Since the adjacency matrix has |V| 2 entries, the amount of spaced needed to represent the edges of a graph is O( |V| 2 ) regardless of the actual number of edges in the graph.Since the adjacency matrix has |V| 2 entries, the amount of spaced needed to represent the edges of a graph is O( |V| 2 ) regardless of the actual number of edges in the graph. If |E| << |V| 2 then most of the entries are 0If |E| << |V| 2 then most of the entries are 0 Wasteful representation !Wasteful representation !

24 Representation > Adjacency Matrix... Sparse and Dense Graphs Definition A sparse graph is a graph G = (V, E) in which |E| = O( |V| ) Definition A dense graph is a graph G = (V, E) in which |E| =  ( |V| 2 )

25 Representation > Adjacency Matrix... Adjacency Lists

26 Representation Comparison of Representations operationadjacency matrixadjacency 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| )

27Implementation(*)

28 Implementation (*) Vertex import java.util.Enumeration; public interface Vertex extends Comparable { int getNumber(); Object getWeight(); Enumeration getIncidentEdges(); Enumeration getEmanatingEdges(); Enumeration getPredecessors(); Enumeration getSuccessors(); }

29 Implementation (*) Edge public interface Edge extends Comparable { Vertex getV0(); Vertex getV1(); Object getWeight(); boolean isDirected(); Vertex getMate(Vertex vertex); }

30 Implementation (*) Graph 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 (*) Directed Graph public interface Digraph extends Graph { boolean isStronglyConnected(); void topologicalOrderTraversal(Visitor visitor); }

32 Graph Traversal

33 Graph Traversal Depth-First Traversal similar to depth-first tree traversalsimilar to depth-first tree traversal because there is not root, we must specify a starting nodebecause there is not root, we must specify a starting node because a graph may be cyclic, we must prevent infinite recursionbecause a graph may be cyclic, we must prevent infinite recursion

34 Graph Traversal Depth-First Traversal... The depth-first traversal visits the nodes in the order c, a, b, d Remark 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 tree.if we omit the edges that are not followed, the remaining edges form a tree. The depth-first traversal of this tree is equivalent to the depth-first traversal of the graphThe depth-first traversal of this tree is equivalent to the depth-first traversal of the graph

35 Graph Traversal > Depth-First Traversal... 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 Breadth-First Traversal similar to breadth-first tree traversalsimilar to breadth-first tree traversal uses a queue of verticesuses a queue of vertices because there is not root, we must specify a starting nodebecause there is not root, we must specify a starting node because a graph may be cyclic, we must ensure that a vertex is enqueued only oncebecause a graph may be cyclic, we must ensure that a vertex is enqueued only once

37 Graph Traversal Breadth-First Traversal... The breadth-first traversal visits the nodes in the order a, b, c, d

38 Graph Traversal > Breadth-First Traversal... 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 Traversal Topological Sort Definition Consider a DAG with G = (V, E). A topological sort of the vertices of G is a sequence S = { v 1, v 2,..., v |V| } in which each element of V appears exactly once. For every pair of distinct vertices v i and v j in the sequence S, if v i  v j is an edge in G, i.e., (v i, v j )  E, then i < j.Definition Consider a DAG with G = (V, E). A topological sort of the vertices of G is a sequence S = { v 1, v 2,..., v |V| } in which each element of V appears exactly once. For every pair of distinct vertices v i and v j in the sequence S, if v i  v j is an edge in G, i.e., (v i, v j )  E, then i < j.

40 Graph Traversal Topological Order Traversal a traversal that visits the vertices of a DAG in the order given by the topological sorta traversal that visits the vertices of a DAG in the order given by the topological sort compute the in-degrees of the verticescompute the in-degrees of the vertices repeatedly select a vertex with zero in-degree, visit it, and then “remove it from the graph”repeatedly select a vertex with zero in-degree, visit it, and then “remove it from the graph” S 1 = { a, b, c, d, e, f, g, h, i } S 2 = { a, c, b, f, e, d, h, g, i } S 3 = { a, b, d, e, g, c, f, h, i }... There are many

41 Is it connectedIs it connected Is there any cyclesIs there any cycles Graph Traversal Applications

42 Graph Traversal > Applications Connectedness Definition An undirected graph G = (V, E) is connected if there is a path in G between every pair of vertices in V. The connected sub-graphs of a graph are called connected components.

43 Graph Traversal > Applications Connectedness Definition A directed graph G = (V, E) is strongly connected if there is a path in G between every pair of vertices in V. Definition A directed graph G = (V, E) is weakly connected if the underlying undirected graph Ğ is connected.

44 Graph Traversal > Applications Connectedness Algorithm For all vertices Start from each vertex Start from each vertex Traverse the graph Traverse the graph Mark every visited vertex Mark every visited vertex If there is an unvisited vertex, unconnected If there is an unvisited vertex, unconnected

45 Graph Traversal > Applications Cycle Algorithm Apply topological-order traversal

46 Shortest Path HW: Find the shortest-path from your hometown to any other city in Turkiye employ at least three different techniques, and find the run time complexity using Use the excel chart below for city-distances

47 Shortest Path Distance Distance between two verticesDistance between two vertices –2-9 –3-9 –

48 Shortest Path Weighted Path Length Definition Consider an edge-weighted graph G = (V, E). Let C(v i,v j ) be the weight on the edge connecting v i to v j. A path in G is a non-empty sequence of vertices P = {v 1, v 2,..., v k }. The weighted path length of path P is given by

49 Shortest Path Single-source shortest path Definition Single-source shortest path problem Given an edge-weighted graph G = (V, E) and a vertex v s  V, find the shortest weighted path from v s to every other vertex in V. Remark If all weights are positive, it is well-defined.

50 if the graph is acyclic, the problem is easyif the graph is acyclic, the problem is easy –do a topological order traversal as long as all the edge weights are non- negative the shortest-path problem is well definedas long as all the edge weights are non- negative the shortest-path problem is well defined –a greedy algorithm works (Dijkstra's algorithm) Shortest Path Special Cases non-negative weights

51 if the graph has negative weights (but not negative cost cycles) a solution exists but the greedy algorithm does not workif the graph has negative weights (but not negative cost cycles) a solution exists but the greedy algorithm does not work if the graph has a negative cost cycle, no solution existsif the graph has a negative cost cycle, no solution exists Shortest Path Special Cases negative weights

52 Shortest Path Dijkstra’s Algorithm k v indicates that the shortest path to vertex v is known. Initially false d v the length of the shortest known path from v s to v. Initially . p v The predecessor of vertex v on the shortest path from v s to v. Initially unknown.

53 Shortest Path Dijkstra’s Algorithm 1.From the set of vertices for with k v =false, select the vertex v having the smallest tentative distance d v. 2.Set k v =true 3.For each vertex w adjacent to v for which k v  true, test whether the tentative distance d v is greater than d v +c(v,w). If it is, set d v  d v +c(v,w) and set p v  v. In each pass exactly one vertex has its k v set to true. The algorithm terminates after |V|passes are completed at which time all the shortest paths are known.

54 Shortest Path : Dijkstra’s Algorithm

55 Shortest Path All-pairs source shortest path Definition All-pairs source shortest path problem Given an edge-weighted graph G = (V, E), for each pair of vertices in V find the length of the shortest weighted path between the two vertices. Given an edge-weighted graph G = (V, E), for each pair of vertices in V find the length of the shortest weighted path between the two vertices.

56 Shortest Path with Floyd’s Algorithm The idea: Determine whether a path going from Vi to Vj via Vk is shorter than the best-known path from Vi to Vk. The idea: Determine whether a path going from Vi to Vj via Vk is shorter than the best-known path from Vi to Vk. V k the first k vertices in V P k (v,w) the shortest path from vertex v to w that passes only through vertices in V k D k (v,w) the length of path P k (v;w)

57 Shortest Path : Floyd’s Algorithm D 0 (v,w) is the adjacency matrix compute the sequence of matrices D 0, D 1,..., D |V| obtain the distances in D i+1 from those in D i by considering only the paths that pass through vertex v i+1 : D i+1 (v;w) = min{ D i (v, v i+1 )+D i (v i+1, w);D i (v,w)} D i+1 (v;w) = min{ D i (v, v i+1 )+D i (v i+1, w);D i (v,w)} HWLA1: Search, Floyds algorithm with O (V^3) HWLA2: Read,

58 Minimum Cost Spanning Trees

59 Minimum Cost Spanning Trees Spanning Tree Definition Consider a connected, undirected graph G=(V, E). A spanning tree T = (V’, E’) of G is a subgraph of G with the following properties: 1.V’ = V 2.T is connected 3.T is acyclic Remark Spanning tree is a tree.

60 Minimum Cost Spanning Trees Total Cost Definition The total cost of an edge-weighted undirected graph is the sum of the weights on all the edges in that graph

61 Minimum Cost Spanning Trees Minimum Spanning Tree Definition Consider an edge-weighted, undirected, connected graph G=(V, E), where c(v, w) represents the weight on edge {v, w}  E. The minimum spanning tree of G is the spanning tree T=(V, E’) that has the smallest total cost, (a simple idea: remove the edges with large weights without loosing connection)

62 Minimum Cost Spanning Trees: Prim 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 Trees Prim’s Algorithm

64 Minimum Cost Spanning Tree: Kruskal 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

65 Critical Path Analysis(*)

66 Eg. Satellite Systems 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 Architecture 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(*)

68 Critical Path Analysis Activity-Node Graphs represent a set of activities and scheduling constraints using a graphrepresent a set of activities and scheduling constraints using a graph each vertex represents an activityeach vertex represents an activity the weight on the vertex represents the time required to complete the activitythe weight on the vertex represents the time required to complete the activity directed edges represent the sequencing constraintsdirected edges represent the sequencing constraints the graph must be acyclicthe graph must be acyclic

69 Critical Path Analysis Critical path analysis Questions What is the minimum amount of time needed to complete all activities?What is the minimum amount of time needed to complete all activities? For a given activity v, is it possible to delay the completion of that activity without affecting the overall completion time?For a given activity v, is it possible to delay the completion of that activity without affecting the overall completion time? If yes, by how much can the completion of activity v be delayed?If yes, by how much can the completion of activity v be delayed?

70 Critical Path Analysis Event-Node Graphs edges represent the activitiesedges represent the activities weights on edges represent the time required for the activitiesweights on edges represent the time required for the activities vertices represent the commencement and termination of activities i.e., eventsvertices represent the commencement and termination of activities i.e., events

71 Critical Path Analysis Earliest and Latest Event Times E v the earliest time at which event v can occur L v the latest time at which event v can occur

72 Critical Path Analysis Slack Time consider an activity represented by edge (v, w)consider an activity represented by edge (v, w) the amount of time available for the activity is L w - E vthe amount of time available for the activity is L w - E v the time required for that activity is c(v, w)the time required for that activity is c(v, w) the slack time for activity (v, w) is S(v,w) = L w - E v - c(v, w)the slack time for activity (v, w) is S(v,w) = L w - E v - c(v, w)

73 Critical Path Analysis Critical Activity and Path Definition An activity with zero slack is critical Definition A critical path is a path from the initial vertex to the final vertex comprised solely of critical activities

74 Critical Path Analysis Example

75Summary GraphGraph –Directed Graph –Undirected Graph –Representation –Implementation 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, 30

76References Introductory Graph Theory Chartrand Dover, 1977Introductory Graph Theory Chartrand Dover, 1977 Data Structures and Algorithms with Object-Oriented Design Patterns in Java Preiss Structures and Algorithms with Object-Oriented Design Patterns in Java Preiss Data Structures and Algorithms with Object-Oriented Design Patters in C++ Preiss Wiley, 1999Data Structures and Algorithms with Object-Oriented Design Patters in C++ Preiss Wiley, 1999 Data Structures and Algorithm Analysis in Java Weiss Addison-Wesley, 1999Data Structures and Algorithm Analysis in Java Weiss Addison-Wesley, 1999