You are on page 1of 1

45

L ECTURE 22
Graphs Nodes/ vertices connected by edges/ arcs, that may have weights/ costs Directed versus undirected graphs depending on whether the edges are ordered or not + Math notation: G = (V, E), with E V V , and cost function c : E R (or R , or N, etc.) Lots of terminology and notation Sec 9.1 (page 359) of the text learn them!! Some terms you should know: Adjacent vertices; digraphs; path; length of a path; simple paths; cycles: simple and non-simple. Directed acyclic graphs (DAGs); connected graphs; complete graphs Example applications include airline routes (each airport a node; non-stop ights are edges). Trafc ow in cities (each intersection a node). Computer Networks. The Graph ADT

In comparing efciency of algorithms (big-Oh stuff), we characterize by two parameters: |V | and |E|. So for example, may have an algorithm that is of complexity O(|E| log |V |) For any graph G = (V, E), 0 |E| |V |2 . dense versus sparse graphs in a dense graph, |E| = (|V |2 ) Representation: As a diagram. (Example: Fig 9.1 p 360- of your text) Computer representation: two commonly-used possibilities: 1) Adjacency Matrix. O(|V |2 ) memory may be too much for bounded-degree graphs (e.g., trafc networks). Note that every bounded-degree graph is sparse! |E| = k |V | for the bound k. 2) Adjacency List. O(|V | + |E|) memory. No signicance to the order in which vertices are listed in each list In an undirected graph, each edge is represented twice 3) Memory efciency is only one basis for comparison. Others: different kinds of questions are answered more efciently by the different representations. Does a particular edge (i, j) exist? Is there a sink vertex in the graph? 4) Our approach: we will describe algorithms at a high level, and then try and choose the best representation. In actual applications (e.g., airline route-maps), vertices usually have names; matrices/ arrays are indexed by non-negative integers. So, need rapid translation from names to numbers constant-time translation can be achieved by, e.g., cuckoo hashing

Topological sort

Only dened for directed acyclic graphs (DAGs) Denition: An ordering of the vertices such that if (i, j) is an edge in the graph then i appears before j in the ordering Motivation: project charts and the like important in management/ logistics/ planning A high-level description of the algorithm The indegree of a vertex: the number of incoming edges. Determine the indegree of each vertex in the graph. Repeat Write out a vertex of indegree zero Update the indegrees resulting from deleting this vertex (and all edges of which it is an end-point) until each vertex has been listed Analysis of the algorithm What does it mean if we cannot nd a vertex of indegree zero? How many times will the repeat-until loop execute?

You might also like