
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 4.2 DIRECTED GRAPHS ‣ introduction ‣ digraph API Algorithms ‣ digraph search FOURTH EDITION ‣ topological sort ‣ strong components ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu 4.2 DIRECTED GRAPHS ‣ introduction ‣ digraph API Algorithms ‣ digraph search ‣ topological sort ‣ strong components ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu Directed graphs Digraph. Set of vertices connected pairwise by directed edges. outdegree = 4 indegree = 2 0 6 8 7 directed path 2 from 0 to 2 1 3 9 10 4 directed cycle 5 11 12 3 Road network To see all the details that are visible on the screen,use the Address Holland Tunnel "Print" link next to the map. New York, NY 10013 Vertex = intersection; edge = one-way street. ©2008 Google - Map data ©2008 Sanborn, NAVTEQ™ - Terms of Use 4 Political blogosphere graph Vertex = political blog; edge = link. The Political Blogosphere and the 2004 U.S. Election: Divided They Blog, Adamic and Glance, 2005 Figure 1: Community structure of political blogs (expanded set), shown using utilizing a GEM layout [11] in the GUESS[3] visualization and analysis tool. The colors reflect political orientation, 5 red for conservative, and blue for liberal. Orange links go from liberal to conservative, and purple ones from conservative to liberal. The size of each blog reflects the number of other blogs that link to it. longer existed, or had moved to a different location. When looking at the front page of a blog we did not make a distinction between blog references made in blogrolls (blogroll links) from those made in posts (post citations). This had the disadvantage of not differentiating between blogs that were actively mentioned in a post on that day, from blogroll links that remain static over many weeks [10]. Since posts usually contain sparse references to other blogs, and blogrolls usually contain dozens of blogs, we assumed that the network obtained by crawling the front page of each blog would strongly reflect blogroll links. 479 blogs had blogrolls through blogrolling.com, while many others simply maintained a list of links to their favorite blogs. We did not include blogrolls placed on a secondary page. We constructed a citation network by identifying whether a URL present on the page of one blog references another political blog. We called a link found anywhere on a blog’s page, a “page link” to distinguish it from a “post citation”, a link to another blog that occurs strictly within a post. Figure 1 shows the unmistakable division between the liberal and conservative political (blogo)spheres. In fact, 91% of the links originating within either the conservative or liberal communities stay within that community. An effect that may not be as apparent from the visualization is that even though we started with a balanced set of blogs, conservative blogs show a greater tendency to link. 84% of conservative blogs link to at least one other blog, and 82% receive a link. In contrast, 74% of liberal blogs link to another blog, while only 67% are linked to by another blog. So overall, we see a slightly higher tendency for conservative blogs to link. Liberal blogs linked to 13.6 blogs on average, while conservative blogs linked to an average of 15.1, and this difference is almost entirely due to the higher proportion of liberal blogs with no links at all. Although liberal blogs may not link as generously on average, the most popular liberal blogs, Daily Kos and Eschaton (atrios.blogspot.com), had 338 and 264 links from our single-day snapshot 4 Overnight interbank loan graph Vertex = bank; edge = overnight loan. GSCC GWCC GIN GOUT DC Tendril 4b`tqX #$ 4XWXqTe YtgWr gXsvhqd Yhq GXisXfUXq # 5Q11 & `bTgs vXTdex VhggXVsXW VhfihgXgs 21 & WbrVhggXVsXW VhfihgXgsThe Topology 5G11 of &the `bTgs Federal rsqhg`ex Funds VhggXVsXW Market, VhfihgXgsBech and Atalay, 57B &2008 `bTgs bgVhfihgXgs 5CIH & `bTgs hts VhfihgXgs Cg sabr WTx saXqX vXqX ! ghWXr bg saX 5G11 ghWXr bg saX 57B ! 6 ghWXr bg 5CIH ghWXr bg saX sXgWqber TgW ghWXr bg T WbrVhggXVsXW VhfihgXgs !"#$%&%'$( HaX ghWXr hY T gXsvhqd VTg UX iTqsbsbhgXW bgsh T VheeXVsbhg hY Wbrchbgs rXsr VTeeXW WbrVhggXVsXW VhfihgXgsr !!"# !!" HaX ghWXr vbsabg XTVa WbrVhggXVsXW VhfihgXgs Wh ghs aTuX ebgdr sh hq Yqhf ! "!!! ghWXr bg Tgx hsaXq VhfihgXgs bX !0 1" !1 0"$0 !!!" 1 !! !!" # bY ) # )! HaX VhfihgXgs vbsa saX eTq`Xrs gtfUXq hY ghWXr br qXYXqqXW# sh Tr saX$!)%*$& +"*,-.% $! /'$$"/&"0& /'12'$"$&' ( O5Q11P 7g hsaXq vhqWr saX 5Q11 br saX eTq`Xrs VhfihgXgs hY saX gXsvhqd bg vabVa Tee ghWXr VhggXVs sh XTVa hsaXq ubT tgWbqXVsXW iTsar HaX qXfTbgbg` WbrVhggXVsXW VhfihgXgsr O21rP TqX rfTeeXq VhfihgXgsr Yhq vabVa saX rTfX br sqtX 7g XfibqbVTe rstWbXr saX 5Q11 br hYsXg YhtgW sh UX rXuXqTe hqWXqr hY fT`gbstWX eTq`Xq saTg Tgx hY saX 21r OrXX 0qhWXq "& *-3 OPP HaX 5Q11 Vhgrbrsr hY T )%*$& 4&5'$)-. /'$$"/&"0 /'12'$"$& O5G11P T )%*$& '6&7/'12'$"$& O5CIHP T )%*$& %$7/'12'$"$& O57BP TgW &"$05%-4 OrXX 4b`tqX #P HaX 5G11 VhfiqbrXr Tee ghWXr saTs VTg qXTVa XuXqx hsaXq ghWX bg saX 5G11 saqht`a T WbqXVsXW iTsa ) ghWX br bg saX 5CIH bY bs aTr T iTsa Yqhf saX 5G11 Uts ghs sh saX 5G11 7g VhgsqTrs T ghWX br bg saX 57B bY bs aTr T iTsa sh saX 5G11 Uts ghs Yqhf bs ) ghWX br bg T sXgWqbe bY bs WhXr ghs qXrbWX hg T WbqXVsXW iTsa sh hq Yqhf saX 5G11 !%4/644%'$( 7g saX gXsvhqd hY iTxfXgsr rXgs huXq 4XWvbqX TgTexyXW Ux GhqTfdb "& *-3 O P saX 5G11 br saX eTq`Xrs VhfihgXgs Cg TuXqT`X Tefhrs %&' hY saX ghWXr bg saTs gXsvhqd UXehg` sh saX 5G11 7g VhgsqTrs saX 5G11 br ftVa rfTeeXq Yhq saX YXWXqTe YtgWr gXsvhqd 7g hgex (&' (' hY saX ghWXr UXehg` sh sabr VhfihgXgs 0x YTq saX eTq`Xrs VhfihgXgs br saX 57B 7g )%' )')hY saX ghWXr vXqX bg sabr VhfihgXgs HaX 5CIH VhgsTbgXW (*' +' hY Tee ghWXr iXq WTx vabeX saXqX) vXqX (+' ,' hY saX ghWXr ehVTsXW bg saX sXgWqber @Xrr saTg -') (' hY saX ghWXr vXqX bg saX qXfTbgbg` WbrVhggXVsXW) VhfihgXgsr OrXX HTUeX P ) HaX sXgWqber fTx Terh UX WbXqXgsbTsXW bgsh saqXX rtUVhfihgXgsr$ T rXs hY ghWXr saTs TqX hg T iTsa XfTgTsbg` Yqhf 57B T rXs hY ghWXr saTs TqX hg T iTsa eXTWbg` sh 5CIH TgW T rXs hY ghWXr saTs TqX hg T iTsa saTs UX`bgr bg 57B TgW XgWr bg 5CIH !!"# hY ghWXr vXqX bg Yqhf57B sXgWqber $!%# hY ghWXr vXqX bg saX sh5CIH sXgWqber TgW "!&# hY ghWXr vXqX bg stUXr Yqhf 57B sh 5CIH Uber taxi graph Vertex = taxi pickup; edge = taxi ride. http://blog.uber.com/2012/01/09/uberdata-san-franciscomics/ 7 Implication graph Vertex = variable; edge = logical implication. if x5 is true, then x0 is true ~x2 x0 x6 ~x4 x3 ~x5 ~x1 x1 x5 ~x3 x4 ~x6 ~x0 x2 8 Combinational circuit Vertex = logical gate; edge = wire. 9 WordNet graph Vertex = synset; edge = hypernym relationship. event happening occurrence occurrent natural_event miracle act human_acon human_acvity change alteraon modicaon miracle group_acon damage harm ..impairment transion increase forfeit forfeiture acon resistance opposion transgression leap jump saltaon jump leap change demoon variaon moon movement move locomoon travel descent run running jump parachung http://wordnet.princeton.edu dash sprint 10 Digraph applications digraph vertex directed edge transportation street intersection one-way street web web page hyperlink food web species predator-prey relationship WordNet synset hypernym scheduling task precedence constraint financial bank transaction cell phone person placed call infectious disease person infection game board position legal move citation journal article citation object graph object pointer inheritance hierarchy class inherits from control flow code block jump 11 Some digraph problems problem description s→t path Is there a path from s to t ? shortest s→t path What is the shortest path from s to t ? directed cycle Is there a directed cycle in the graph ? topological sort Can the digraph be drawn so that all edges point upwards? strong connectivity Is there a directed path between all pairs of vertices ? transitive closure For which vertices v and w is there a directed path from v to w ? PageRank What is the importance of a web page ? 12 4.2 DIRECTED GRAPHS ‣ introduction ‣ digraph API Algorithms ‣ digraph search ‣ topological sort ‣ strong components ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu Digraph API Almost identical to Graph API. public class Digraph Digraph(int V) create an empty digraph with V vertices Digraph(In in) create a digraph from input stream void addEdge(int v, int w) add a directed edge v→w Iterable<Integer> adj(int v) vertices pointing from v int V() number of vertices int E() number of edges Digraph reverse() reverse of this digraph String toString() string representation 14 Digraph API % java Digraph tinyDG.txt tinyDG.txt V 0->5 5 1 13 E 22 0->1 4 2 2->0 adj[] 2 3 2->3 0 3 3 2 0 6 0 3->5 1 5 2 0 1 3->2 2 0 2 4->3 11 12 3 2 3 12 9 4->2 9 10 4 5->4 4 9 11 5 7 9 ⋮ 9 4 8 0 10 12 6 11->4 11 4 7 11->12 4 3 6 9 8 3 5 12->9 6 8 9 6 8 6 10 ⋮5 4 0 5 11 11 10 6 4 12 6 9 12 In in = new In(args[0]); read digraph from 7 6 input stream Digraph G = new Digraph(in); 4 12 for (int v = 0; v < G.V(); v++) 9 print out each for (int w : G.adj(v)) edge (once) StdOut.println(v + "->" + w); 15 Digraph representation: adjacency lists Maintain vertex-indexed array of lists.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages66 Page
-
File Size-