4.2 Directed Graphs 4.2 Directed Graphs

4.2 Directed Graphs 4.2 Directed Graphs

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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us