├── LICENSE ├── Makefile ├── README.md ├── dub.json ├── source └── dgraph │ ├── graph.d │ ├── metric.d │ └── test │ ├── samplegraph10k.d │ ├── samplegraph50.d │ └── tests.d └── util ├── betweenness10k └── source │ └── betweenness10k.d ├── betweenness50 └── source │ └── betweenness50.d └── graphtest └── source └── graphtest.d /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | DC = gdmd 2 | DFLAGS = -O -inline 3 | LIBSRC = source/dgraph/*.d source/dgraph/test/*.d 4 | PROGS = dgraph_graphtest dgraph_betweenness50 dgraph_betweenness10k 5 | 6 | all: $(PROGS) 7 | 8 | docs: $(LIBSRC) 9 | $(DC) -o- -D -Dddocs $(LIBSRC) 10 | 11 | dgraph_%: util/*/source/%.d $(LIBSRC) 12 | $(DC) $(DFLAGS) -ofdgraph_$* util/$*/source/$*.d $(LIBSRC) 13 | 14 | .PHONY: clean 15 | 16 | clean: 17 | rm -f $(PROGS) *.o *.di 18 | 19 | docs-clean: 20 | rm -rf docs 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Dgraph 2 | ====== 3 | 4 | Dgraph is a library for creating, analysing and manipulating graphs, written in 5 | the D programming language. It aims to be fast and memory-efficient while also 6 | being easy to use and extend. 7 | 8 | The project is in very early experimental stages of development, so breaking 9 | changes may occur on a regular basis (although every effort will be made to 10 | ensure that such breakage is justified by the gains). News and updates on 11 | the project will be published on the author's blog: http://braingam.es/ 12 | 13 | Git repository: https://github.com/WebDrake/Dgraph 14 | 15 | Dgraph is distributed under the terms of the GNU General Public License, 16 | version 3 or (at your option) any later version. 17 | 18 | 19 | Features 20 | -------- 21 | 22 | Dgraph currently implements two different graph types (module dgraph.graph): 23 | ```IndexedEdgeList``` is an adaptation to D of the similarly-named igraph 24 | data type, while ```CachedEdgeList``` is an extension of the indexed edge 25 | list that (as its name indicates) caches the results of various calculations 26 | in order to provide faster performance. 27 | 28 | Graphs may be directed or undirected, but Dgraph currently offers no support 29 | for weighted graphs or any other edge or vertex properties. Arbitrary vertex 30 | IDs (e.g. strings) are not currently supported. 31 | 32 | The module dgraph.metric offers a selection of metrics for calculating 33 | different graph properties: currently betweenness centrality and largest 34 | connected cluster size are implemented. 35 | 36 | Finally, the library provides a small selection of benchmarks for graph 37 | construction and calculation of graph metrics. 38 | 39 | 40 | Building 41 | -------- 42 | 43 | Dgraph is a source library and so does not need to be compiled in order to use: 44 | just import the modules into your own D program. DUB packaging is supported 45 | and can be used to build programs that have Dgraph as a dependency 46 | (see http://code.dlang.org/ for more information). 47 | 48 | Several test utilities are provided that offer benchmarking of key features. 49 | These can be built either using make (`make all` or `make [name]`) or with 50 | dub (`dub build dgraph:[name]`). The currently available utilities are as 51 | follows: 52 | 53 | * __dgraph_graphtest__ benchmarks the creation of graphs from scratch, using 54 | two sample graphs with 50 and 10,000 nodes respectively. 55 | 56 | * __dgraph_betweenness50__ benchmarks betweenness centrality calculation on 57 | the 50-node sample graph. 58 | 59 | * __dgraph_betweenness10k__ benchmarks betweenness centrality calculation on 60 | the 10,000-node sample graph. 61 | 62 | 63 | Contributing 64 | ------------ 65 | 66 | Code contributions to Dgraph are welcome. Please try to follow the D style 67 | guidelines: http://dlang.org/dstyle.html 68 | 69 | Feature requests and bug reports can be submitted via the Dgraph GitHub 70 | issue list. 71 | -------------------------------------------------------------------------------- /dub.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dgraph", 3 | "description": "A library for creating, analysing and manipulating graphs (networks). It aims to be fast and memory-efficient while also being easy to use and extend.", 4 | "authors": ["Joseph Rushton Wakeling"], 5 | "copyright": "Copyright © 2013 Joseph Rushton Wakeling", 6 | "homepage": "https://github.com/WebDrake/Dgraph", 7 | "license": "GPL-3.0 or later", 8 | "targetType": "sourceLibrary", 9 | 10 | "subPackages": [ 11 | { 12 | "name": "graphtest", 13 | "targetType": "executable", 14 | "sourcePaths": ["util/graphtest/source/"], 15 | "dependencies": { "dgraph": ">=0.0.1" } 16 | }, 17 | 18 | { 19 | "name": "betweenness50", 20 | "targetType": "executable", 21 | "sourcePaths": ["util/betweenness50/source/"], 22 | "dependencies": { "dgraph": ">=0.0.1" } 23 | }, 24 | 25 | { 26 | "name": "betweenness10k", 27 | "targetType": "executable", 28 | "sourcePaths": ["util/betweenness10k/source/"], 29 | "dependencies": { "dgraph": ">=0.0.1" } 30 | } 31 | 32 | ] 33 | } 34 | -------------------------------------------------------------------------------- /source/dgraph/graph.d: -------------------------------------------------------------------------------- 1 | // Written in the D programming language. 2 | 3 | /** 4 | Basic graph data structures. 5 | 6 | Authors: $(LINK2 http://braingam.es/, Joseph Rushton Wakeling) 7 | Copyright: Copyright © 2013 Joseph Rushton Wakeling 8 | License: This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see $(LINK http://www.gnu.org/licenses/). 20 | 21 | Credits: The basic graph data structure used here is adapted from the library 22 | $(LINK2 http://igraph.sourceforge.net/, igraph) by Gábor Csárdi and 23 | Tamás Nepusz. 24 | */ 25 | 26 | module dgraph.graph; 27 | 28 | import std.algorithm, std.array, std.exception, std.range, std.traits; 29 | import std.string : format; 30 | 31 | /// Test if G is a Dgraph graph type. 32 | template isGraph(G) 33 | { 34 | static if (!__traits(hasMember, G, "directed") || 35 | !__traits(hasMember, G, "edge") || 36 | !__traits(hasMember, G, "edgeCount") || 37 | !__traits(hasMember, G, "vertexCount") || 38 | !__traits(hasMember, G, "isEdge") || 39 | !__traits(hasMember, G, "edgeID") || 40 | !__traits(hasMember, G, "addEdge") || 41 | !__traits(hasMember, G, "degreeIn") || 42 | !__traits(hasMember, G, "degreeOut") || 43 | !__traits(hasMember, G, "incidentEdgesIn") || 44 | !__traits(hasMember, G, "incidentEdgesOut") || 45 | !__traits(hasMember, G, "neighboursIn") || 46 | !__traits(hasMember, G, "neighboursOut")) 47 | { 48 | enum bool isGraph = false; 49 | } 50 | else static if (!isBoolean!(typeof(G.directed))) 51 | { 52 | enum bool isGraph = false; 53 | } 54 | else static if (G.directed && (__traits(hasMember, G, "degree") || 55 | __traits(hasMember, G, "incidentEdges") || 56 | __traits(hasMember, G, "neighbours"))) 57 | { 58 | enum bool isGraph = false; 59 | } 60 | else static if (!G.directed && (!__traits(hasMember, G, "degree") || 61 | !__traits(hasMember, G, "incidentEdges") || 62 | !__traits(hasMember, G, "neighbours"))) 63 | { 64 | enum bool isGraph = false; 65 | } 66 | else static if (!isRandomAccessRange!(ReturnType!(G.incidentEdgesIn)) || 67 | !isRandomAccessRange!(ReturnType!(G.incidentEdgesOut))) 68 | { 69 | enum bool isGraph = false; 70 | } 71 | else static if (!isRandomAccessRange!(ReturnType!(G.neighboursIn)) || 72 | !isRandomAccessRange!(ReturnType!(G.neighboursOut))) 73 | { 74 | enum bool isGraph = false; 75 | } 76 | else static if (!G.directed && (!isRandomAccessRange!(ReturnType!(G.incidentEdges)) || 77 | !isRandomAccessRange!(ReturnType!(G.neighbours)))) 78 | { 79 | enum bool isGraph = false; 80 | } 81 | else 82 | { 83 | enum bool isGraph = true; 84 | } 85 | } 86 | 87 | /// Test if G is a directed graph. 88 | template isDirectedGraph(G) 89 | { 90 | static if (isGraph!G) 91 | { 92 | enum bool isDirectedGraph = G.directed; 93 | } 94 | else 95 | { 96 | enum bool isDirectedGraph = false; 97 | } 98 | } 99 | 100 | /// Test if G is an undirected graph. 101 | template isUndirectedGraph(G) 102 | { 103 | static if (isGraph!G) 104 | { 105 | enum bool isUndirectedGraph = !G.directed; 106 | } 107 | else 108 | { 109 | enum bool isUndirectedGraph = false; 110 | } 111 | } 112 | 113 | unittest 114 | { 115 | assert(isGraph!(IndexedEdgeList!true)); 116 | assert(isGraph!(IndexedEdgeList!false)); 117 | assert(isDirectedGraph!(IndexedEdgeList!true)); 118 | assert(!isDirectedGraph!(IndexedEdgeList!false)); 119 | assert(!isUndirectedGraph!(IndexedEdgeList!true)); 120 | assert(isUndirectedGraph!(IndexedEdgeList!false)); 121 | 122 | assert(isGraph!(CachedEdgeList!true)); 123 | assert(isGraph!(CachedEdgeList!false)); 124 | assert(isDirectedGraph!(CachedEdgeList!true)); 125 | assert(!isDirectedGraph!(CachedEdgeList!false)); 126 | assert(!isUndirectedGraph!(CachedEdgeList!true)); 127 | assert(isUndirectedGraph!(CachedEdgeList!false)); 128 | } 129 | 130 | /** 131 | * Graph data type based on igraph's igraph_t. The basic data structure is a 132 | * pair of arrays whose entries consist of respectively the source (tail) and 133 | * destination (head) vertices of the edges in the graph. These are extended 134 | * by sorted indices and cumulative sums that enable fast calculation of graph 135 | * properties from the stored data. 136 | */ 137 | final class IndexedEdgeList(bool dir) 138 | { 139 | private: 140 | size_t[] tail_; 141 | size_t[] head_; 142 | size_t[] indexTail_; 143 | size_t[] indexHead_; 144 | size_t[] sumTail_ = [0]; 145 | size_t[] sumHead_ = [0]; 146 | 147 | void indexEdgesInsertion() 148 | { 149 | assert(indexTail_.length == indexHead_.length); 150 | assert(tail_.length == head_.length); 151 | immutable size_t l = indexTail_.length; 152 | indexTail_.length = tail_.length; 153 | indexHead_.length = head_.length; 154 | foreach (immutable e; l .. tail_.length) 155 | { 156 | size_t i, j, lower, upper; 157 | upper = indexTail_[0 .. e].map!(a => tail_[a]).assumeSorted.lowerBound(tail_[e] + 1).length; 158 | lower = indexTail_[0 .. upper].map!(a => tail_[a]).assumeSorted.lowerBound(tail_[e]).length; 159 | i = lower + indexTail_[lower .. upper].map!(a => head_[a]).assumeSorted.lowerBound(head_[e]).length; 160 | for (j = e; j > i; --j) 161 | { 162 | indexTail_[j] = indexTail_[j - 1]; 163 | } 164 | indexTail_[i] = e; 165 | 166 | upper = indexHead_[0 .. e].map!(a => head_[a]).assumeSorted.lowerBound(head_[e] + 1).length; 167 | lower = indexHead_[0 .. upper].map!(a => head_[a]).assumeSorted.lowerBound(head_[e]).length; 168 | i = lower + indexHead_[lower .. upper].map!(a => tail_[a]).assumeSorted.lowerBound(tail_[e]).length; 169 | for (j = e; j > i; --j) 170 | { 171 | indexHead_[j] = indexHead_[j - 1]; 172 | } 173 | indexHead_[i] = e; 174 | } 175 | assert(indexTail_.length == indexHead_.length); 176 | assert(indexTail_.length == tail_.length, format("%s tail indices but %s tail values.", indexTail_.length, tail_.length)); 177 | assert(indexHead_.length == head_.length, format("%s head indices but %s head values.", indexHead_.length, head_.length)); 178 | } 179 | 180 | void indexEdgesSort() 181 | { 182 | indexTail_ ~= iota(indexTail_.length, tail_.length).array; 183 | indexHead_ ~= iota(indexHead_.length, head_.length).array; 184 | assert(indexTail_.length == indexHead_.length); 185 | indexTail_.multiSort!((a, b) => tail_[a] < tail_[b], (a, b) => head_[a] < head_[b]); 186 | indexHead_.multiSort!((a, b) => head_[a] < head_[b], (a, b) => tail_[a] < tail_[b]); 187 | } 188 | 189 | void sumEdges(ref size_t[] sum, in size_t[] vertex, in size_t[] index) @safe const nothrow pure 190 | { 191 | assert(sum.length > 1); 192 | 193 | size_t v = vertex[index[0]]; 194 | sum[0 .. v + 1] = 0; 195 | for (size_t i = 1; i < index.length; ++i) 196 | { 197 | size_t n = vertex[index[i]] - vertex[index[sum[v]]]; 198 | sum[v + 1 .. v + n + 1] = i; 199 | v += n; 200 | } 201 | sum[v + 1 .. $] = vertex.length; 202 | } 203 | 204 | public: 205 | /** 206 | * Add new edges to the graph. These may be provided either singly, by 207 | * passing an individual (tail, head) pair, or en masse by passing an array 208 | * whose entries are [tail1, head1, tail2, head2, ...]. Duplicate edges 209 | * are permitted. 210 | */ 211 | void addEdge()(size_t tail, size_t head) 212 | { 213 | enforce(tail < vertexCount, format("Edge tail %s is greater than largest vertex ID %s", tail, vertexCount - 1)); 214 | enforce(head < vertexCount, format("Edge head %s is greater than largest vertex ID %s", head, vertexCount - 1)); 215 | static if (!directed) 216 | { 217 | if (head < tail) 218 | { 219 | swap(tail, head); 220 | } 221 | } 222 | tail_ ~= tail; 223 | head_ ~= head; 224 | indexEdgesInsertion(); 225 | ++sumTail_[tail + 1 .. $]; 226 | ++sumHead_[head + 1 .. $]; 227 | } 228 | 229 | /// ditto 230 | void addEdge(T : size_t)(T[] edgeList) 231 | { 232 | enforce(edgeList.length % 2 == 0); 233 | assert(tail_.length == head_.length); 234 | immutable size_t l = tail_.length; 235 | tail_.length += edgeList.length / 2; 236 | head_.length += edgeList.length / 2; 237 | foreach (immutable i; 0 .. edgeList.length / 2) 238 | { 239 | size_t tail = edgeList[2 * i]; 240 | size_t head = edgeList[2 * i + 1]; 241 | enforce(tail < vertexCount, format("Edge tail %s is greater than largest vertex ID %s", tail, vertexCount - 1)); 242 | enforce(head < vertexCount, format("Edge head %s is greater than vertex count %s", head, vertexCount - 1)); 243 | static if (!directed) 244 | { 245 | if (head < tail) 246 | { 247 | swap(tail, head); 248 | } 249 | } 250 | tail_[l + i] = tail; 251 | head_[l + i] = head; 252 | } 253 | indexEdgesSort(); 254 | sumEdges(sumTail_, tail_, indexTail_); 255 | sumEdges(sumHead_, head_, indexHead_); 256 | } 257 | 258 | static if (directed) 259 | { 260 | /** 261 | * Provide respectively the in- and out-degrees of a vertex v, i.e. 262 | * the number of vertices to which v is connected by respectively 263 | * incoming or outgoing links. If the graph is undirected, these 264 | * values are identical and the general degree method is also defined. 265 | */ 266 | size_t degreeIn(in size_t v) @safe const pure 267 | { 268 | enforce(isVertex(v)); 269 | assert(v + 1 < sumHead_.length); 270 | return sumHead_[v + 1] - sumHead_[v]; 271 | } 272 | 273 | ///ditto 274 | size_t degreeOut(in size_t v) @safe const pure 275 | { 276 | enforce(isVertex(v)); 277 | assert(v + 1 < sumTail_.length); 278 | return sumTail_[v + 1] - sumTail_[v]; 279 | } 280 | } 281 | else 282 | { 283 | /// Provides the degree of a vertex v in an undirected graph. 284 | size_t degree(in size_t v) @safe const pure 285 | { 286 | enforce(isVertex(v)); 287 | assert(v + 1 < sumTail_.length); 288 | assert(sumTail_.length == sumHead_.length); 289 | return (sumTail_[v + 1] - sumTail_[v]) 290 | + (sumHead_[v + 1] - sumHead_[v]); 291 | } 292 | 293 | alias degreeIn = degree; 294 | alias degreeOut = degree; 295 | } 296 | 297 | /** 298 | * Static boolean value indicating whether or not the graph is directed. 299 | * This is available for compile-time as well as runtime checks. 300 | */ 301 | alias directed = dir; 302 | 303 | /** 304 | * Returns a list of all edges in the graph in the form of a list of 305 | * (tail, head) vertex pairs. 306 | */ 307 | auto edge() @property @safe const nothrow pure 308 | { 309 | return zip(tail_, head_); 310 | } 311 | 312 | /// Total number of edges in the graph. 313 | size_t edgeCount() @property @safe const nothrow pure 314 | { 315 | assert(tail_.length == head_.length); 316 | return tail_.length; 317 | } 318 | 319 | /** 320 | * Returns the edge index for a given (tail, head) vertex pair. If 321 | * (tail, head) is not an edge, will throw an exception. 322 | */ 323 | size_t edgeID(size_t tail, size_t head) const 324 | { 325 | if (!isVertex(tail)) 326 | { 327 | throw new Exception(format("No vertex with ID %s", tail)); 328 | } 329 | 330 | if (!isVertex(head)) 331 | { 332 | throw new Exception(format("No vertex with ID %s", head)); 333 | } 334 | 335 | static if (!directed) 336 | { 337 | if (head < tail) 338 | { 339 | swap(tail, head); 340 | } 341 | } 342 | 343 | size_t tailDeg = sumTail_[tail + 1] - sumTail_[tail]; 344 | size_t headDeg = sumHead_[head + 1] - sumHead_[head]; 345 | 346 | if (tailDeg == 0) 347 | { 348 | static if (directed) 349 | { 350 | assert(degreeOut(tail) == 0); 351 | throw new Exception(format("Vertex %s has no outgoing neighbours.", tail)); 352 | } 353 | else 354 | { 355 | throw new Exception(format("(%s, %s) is not an edge", tail, head)); 356 | } 357 | } 358 | 359 | if (headDeg == 0) 360 | { 361 | static if (directed) 362 | { 363 | assert(degreeIn(head) == 0); 364 | throw new Exception(format("Vertex %s has no incoming neighbours.", head)); 365 | } 366 | else 367 | { 368 | throw new Exception(format("(%s, %s) is not an edge", tail, head)); 369 | } 370 | } 371 | 372 | if (tailDeg < headDeg) 373 | { 374 | // search among the heads of tail 375 | foreach (immutable i; iota(sumTail_[tail], sumTail_[tail + 1]).map!(a => indexTail_[a])) 376 | { 377 | if (head_[i] == head) 378 | { 379 | assert(tail_[i] == tail); 380 | return i; 381 | } 382 | } 383 | throw new Exception(format("(%s, %s) is not an edge.", tail, head)); 384 | } 385 | else 386 | { 387 | // search among the tails of head 388 | foreach (immutable i; iota(sumHead_[head], sumHead_[head + 1]).map!(a => indexHead_[a])) 389 | { 390 | if (tail_[i] == tail) 391 | { 392 | assert(head_[i] == head); 393 | return i; 394 | } 395 | } 396 | throw new Exception(format("(%s, %s) is not an edge.", tail, head)); 397 | } 398 | } 399 | 400 | static if (directed) 401 | { 402 | /** 403 | * Returns the IDs of edges respectively incoming to or outgoing from 404 | * the specified vertex v. If the graph is undirected the two will be 405 | * identical and the general method incidentEdges is also defined. 406 | */ 407 | auto incidentEdgesIn(in size_t v) const 408 | { 409 | enforce(isVertex(v)); 410 | return iota(sumHead_[v], sumHead_[v + 1]).map!(a => indexHead_[a]); 411 | } 412 | 413 | /// ditto 414 | auto incidentEdgesOut(in size_t v) const 415 | { 416 | enforce(isVertex(v)); 417 | return iota(sumTail_[v], sumTail_[v + 1]).map!(a => indexTail_[a]); 418 | } 419 | } 420 | else 421 | { 422 | /// ditto 423 | auto incidentEdges(in size_t v) const 424 | { 425 | enforce(isVertex(v)); 426 | return chain(iota(sumHead_[v], sumHead_[v + 1]).map!(a => indexHead_[a]), 427 | iota(sumTail_[v], sumTail_[v + 1]).map!(a => indexTail_[a])); 428 | } 429 | 430 | alias incidentEdgesIn = incidentEdges; 431 | alias incidentEdgesOut = incidentEdges; 432 | } 433 | 434 | /** 435 | * Checks if a given (tail, head) vertex pair forms an edge in the graph. 436 | */ 437 | bool isEdge(size_t tail, size_t head) const 438 | { 439 | if (!(isVertex(tail) && isVertex(head))) 440 | { 441 | return false; 442 | } 443 | 444 | static if (!directed) 445 | { 446 | if (head < tail) 447 | { 448 | swap(tail, head); 449 | } 450 | } 451 | 452 | size_t tailDeg = sumTail_[tail + 1] - sumTail_[tail]; 453 | if (tailDeg == 0) 454 | { 455 | return false; 456 | } 457 | 458 | size_t headDeg = sumHead_[head + 1] - sumHead_[head]; 459 | if (headDeg == 0) 460 | { 461 | return false; 462 | } 463 | 464 | if (tailDeg < headDeg) 465 | { 466 | // search among the heads of tail 467 | foreach (immutable t; iota(sumTail_[tail], sumTail_[tail + 1]).map!(a => head_[indexTail_[a]])) 468 | { 469 | if (t == head) 470 | { 471 | return true; 472 | } 473 | } 474 | return false; 475 | } 476 | else 477 | { 478 | // search among the tails of head 479 | foreach (immutable h; iota(sumHead_[head], sumHead_[head + 1]).map!(a => tail_[indexHead_[a]])) 480 | { 481 | if (h == tail) 482 | { 483 | return true; 484 | } 485 | } 486 | return false; 487 | } 488 | } 489 | 490 | bool isVertex(T : size_t)(in T v) @safe const nothrow pure 491 | { 492 | static if (isSigned!T) 493 | { 494 | if (v < 0) 495 | { 496 | return false; 497 | } 498 | } 499 | 500 | if (v < vertexCount) 501 | { 502 | return true; 503 | } 504 | 505 | return false; 506 | } 507 | 508 | static if (directed) 509 | { 510 | /** 511 | * Returns the IDs of vertices connected to v via incoming or outgoing 512 | * links. If the graph is undirected the two will be identical and the 513 | * general neighbours method is also defined. 514 | */ 515 | auto neighboursIn(in size_t v) const 516 | { 517 | enforce(isVertex(v)); 518 | return iota(sumHead_[v], sumHead_[v + 1]).map!(a => tail_[indexHead_[a]]); 519 | } 520 | 521 | /// ditto 522 | auto neighboursOut(in size_t v) const 523 | { 524 | enforce(isVertex(v)); 525 | return iota(sumTail_[v], sumTail_[v + 1]).map!(a => head_[indexTail_[a]]); 526 | } 527 | } 528 | else 529 | { 530 | /// ditto 531 | auto neighbours(in size_t v) const 532 | { 533 | enforce(isVertex(v)); 534 | return chain(iota(sumHead_[v], sumHead_[v + 1]).map!(a => tail_[indexHead_[a]]), 535 | iota(sumTail_[v], sumTail_[v + 1]).map!(a => head_[indexTail_[a]])); 536 | } 537 | 538 | alias neighbors = neighbours; 539 | alias neighboursIn = neighbours; 540 | alias neighboursOut = neighbours; 541 | } 542 | 543 | alias neighborsIn = neighboursIn; 544 | alias neighborsOut = neighboursOut; 545 | 546 | /** 547 | * Get or set the total number of vertices in the graph. Will throw an 548 | * exception if resetting the number of vertices would delete edges. 549 | */ 550 | size_t vertexCount() @property @safe const nothrow pure 551 | { 552 | assert(sumTail_.length == sumHead_.length); 553 | return sumTail_.length - 1; 554 | } 555 | 556 | /// ditto 557 | size_t vertexCount(in size_t n) @property @safe pure 558 | { 559 | immutable size_t l = sumTail_.length; 560 | if (n < (l - 1)) 561 | { 562 | // Check that no edges are lost this way 563 | if ((sumTail_[n] != sumTail_[$-1]) || 564 | (sumHead_[n] != sumHead_[$-1])) 565 | { 566 | throw new Exception("Cannot set vertexCount value without deleting edges"); 567 | } 568 | else 569 | { 570 | sumTail_.length = n + 1; 571 | sumHead_.length = n + 1; 572 | } 573 | } 574 | else 575 | { 576 | sumTail_.length = n + 1; 577 | sumHead_.length = n + 1; 578 | sumTail_[l .. $] = sumTail_[l - 1]; 579 | sumHead_[l .. $] = sumHead_[l - 1]; 580 | } 581 | return vertexCount; 582 | } 583 | } 584 | 585 | /** 586 | * An extension of IndexedEdgeList that caches the results of calculations of 587 | * various graph properties so as to provide speedier performance. Provides 588 | * the same set of public methods. This is the recommended data type to use 589 | * with Dgraph. 590 | */ 591 | final class CachedEdgeList(bool dir) 592 | { 593 | private: 594 | IndexedEdgeList!dir graph_; 595 | size_t[] incidentEdgesCache_; 596 | size_t[] neighboursCache_; 597 | 598 | static if (directed) 599 | { 600 | const(size_t)[][] incidentEdgesIn_; 601 | const(size_t)[][] incidentEdgesOut_; 602 | const(size_t)[][] neighboursIn_; 603 | const(size_t)[][] neighboursOut_; 604 | } 605 | else 606 | { 607 | const(size_t)[][] incidentEdges_; 608 | const(size_t)[][] neighbours_; 609 | } 610 | 611 | public: 612 | this() 613 | { 614 | graph_ = new IndexedEdgeList!dir; 615 | } 616 | 617 | alias graph_ this; 618 | 619 | void addEdge()(size_t tail, size_t head) 620 | { 621 | graph_.addEdge(tail, head); 622 | neighboursCache_.length = 2 * tail_.length; 623 | incidentEdgesCache_.length = 2 * tail_.length; 624 | static if (directed) 625 | { 626 | neighboursIn_[] = null; 627 | neighboursOut_[] = null; 628 | incidentEdgesIn_[] = null; 629 | incidentEdgesOut_[] = null; 630 | } 631 | else 632 | { 633 | neighbours_[] = null; 634 | incidentEdges_[] = null; 635 | } 636 | } 637 | 638 | void addEdge(T : size_t)(T[] edgeList) 639 | { 640 | graph_.addEdge(edgeList); 641 | neighboursCache_.length = 2 * tail_.length; 642 | incidentEdgesCache_.length = 2 * tail_.length; 643 | static if (directed) 644 | { 645 | neighboursIn_[] = null; 646 | neighboursOut_[] = null; 647 | incidentEdgesIn_[] = null; 648 | incidentEdgesOut_[] = null; 649 | } 650 | else 651 | { 652 | neighbours_[] = null; 653 | incidentEdges_[] = null; 654 | } 655 | } 656 | 657 | static if (directed) 658 | { 659 | size_t degreeIn(in size_t v) @safe const pure 660 | { 661 | return graph_.degreeIn(v); 662 | } 663 | 664 | size_t degreeOut(in size_t v) @safe const pure 665 | { 666 | return graph_.degreeOut(v); 667 | } 668 | } 669 | else 670 | { 671 | size_t degree(in size_t v) @safe const pure 672 | { 673 | return graph_.degree(v); 674 | } 675 | 676 | alias degreeIn = degree; 677 | alias degreeOut = degree; 678 | } 679 | 680 | alias directed = dir; 681 | 682 | auto edge() @property @safe const nothrow pure 683 | { 684 | return graph_.edge; 685 | } 686 | 687 | size_t edgeCount() @property @safe const nothrow pure 688 | { 689 | return graph_.edgeCount; 690 | } 691 | 692 | size_t edgeID(size_t tail, size_t head) const 693 | { 694 | return graph_.edgeID(tail, head); 695 | } 696 | 697 | static if (directed) 698 | { 699 | auto incidentEdgesIn(in size_t v) @safe nothrow pure 700 | { 701 | if (incidentEdgesIn_[v] is null) 702 | { 703 | immutable size_t start = sumHead_[v] + sumTail_[v]; 704 | immutable size_t end = sumTail_[v] + sumHead_[v + 1]; 705 | size_t j = start; 706 | foreach (immutable i; sumHead_[v] .. sumHead_[v + 1]) 707 | { 708 | incidentEdgesCache_[j] = indexHead_[i]; 709 | ++j; 710 | } 711 | assert(j == end); 712 | incidentEdgesIn_[v] = incidentEdgesCache_[start .. end]; 713 | } 714 | return incidentEdgesIn_[v]; 715 | } 716 | 717 | auto incidentEdgesOut(in size_t v) @safe nothrow pure 718 | { 719 | if (incidentEdgesOut_[v] is null) 720 | { 721 | immutable size_t start = sumTail_[v] + sumHead_[v + 1]; 722 | immutable size_t end = sumHead_[v + 1] + sumTail_[v + 1]; 723 | size_t j = start; 724 | foreach (immutable i; sumTail_[v] .. sumTail_[v + 1]) 725 | { 726 | incidentEdgesCache_[j] = indexTail_[i]; 727 | ++j; 728 | } 729 | assert(j == end); 730 | incidentEdgesOut_[v] = incidentEdgesCache_[start .. end]; 731 | } 732 | return incidentEdgesOut_[v]; 733 | } 734 | } 735 | else 736 | { 737 | auto incidentEdges(in size_t v) @safe nothrow pure 738 | { 739 | if (incidentEdges_[v] is null) 740 | { 741 | immutable size_t start = sumHead_[v] + sumTail_[v]; 742 | immutable size_t end = sumHead_[v + 1] + sumTail_[v + 1]; 743 | size_t j = start; 744 | foreach (immutable i; sumHead_[v] .. sumHead_[v + 1]) 745 | { 746 | incidentEdgesCache_[j] = indexHead_[i]; 747 | ++j; 748 | } 749 | foreach (immutable i; sumTail_[v] .. sumTail_[v + 1]) 750 | { 751 | incidentEdgesCache_[j] = indexTail_[i]; 752 | ++j; 753 | } 754 | assert(j == end); 755 | incidentEdges_[v] = incidentEdgesCache_[start .. end]; 756 | } 757 | return incidentEdges_[v]; 758 | } 759 | 760 | alias incidentEdgesIn = incidentEdges; 761 | alias incidentEdgesOut = incidentEdges; 762 | } 763 | 764 | bool isEdge(size_t tail, size_t head) const 765 | { 766 | return graph_.isEdge(tail, head); 767 | } 768 | 769 | bool isVertex(T : size_t)(in T v) @safe const nothrow pure 770 | { 771 | return graph_.isVertex(v); 772 | } 773 | 774 | static if (directed) 775 | { 776 | auto neighboursIn(in size_t v) @safe nothrow pure 777 | { 778 | if (neighboursIn_[v] is null) 779 | { 780 | immutable size_t start = sumHead_[v] + sumTail_[v]; 781 | immutable size_t end = sumTail_[v] + sumHead_[v + 1]; 782 | size_t j = start; 783 | foreach (immutable i; sumHead_[v] .. sumHead_[v + 1]) 784 | { 785 | neighboursCache_[j] = tail_[indexHead_[i]]; 786 | ++j; 787 | } 788 | assert(j == end); 789 | neighboursIn_[v] = neighboursCache_[start .. end]; 790 | } 791 | return neighboursIn_[v]; 792 | } 793 | 794 | auto neighboursOut(in size_t v) @safe nothrow pure 795 | { 796 | if (neighboursOut_[v] is null) 797 | { 798 | immutable size_t start = sumTail_[v] + sumHead_[v + 1]; 799 | immutable size_t end = sumHead_[v + 1] + sumTail_[v + 1]; 800 | size_t j = start; 801 | foreach (immutable i; sumTail_[v] .. sumTail_[v + 1]) 802 | { 803 | neighboursCache_[j] = head_[indexTail_[i]]; 804 | ++j; 805 | } 806 | assert(j == end); 807 | neighboursOut_[v] = neighboursCache_[start .. end]; 808 | } 809 | return neighboursOut_[v]; 810 | } 811 | } 812 | else 813 | { 814 | auto neighbours(in size_t v) @safe nothrow pure 815 | { 816 | if (neighbours_[v] is null) 817 | { 818 | immutable size_t start = sumHead_[v] + sumTail_[v]; 819 | immutable size_t end = sumHead_[v + 1] + sumTail_[v + 1]; 820 | size_t j = start; 821 | foreach (immutable i; sumHead_[v] .. sumHead_[v + 1]) 822 | { 823 | neighboursCache_[j] = tail_[indexHead_[i]]; 824 | ++j; 825 | } 826 | foreach (immutable i; sumTail_[v] .. sumTail_[v + 1]) 827 | { 828 | neighboursCache_[j] = head_[indexTail_[i]]; 829 | ++j; 830 | } 831 | assert(j == end); 832 | neighbours_[v] = neighboursCache_[start .. end]; 833 | } 834 | return neighbours_[v]; 835 | } 836 | 837 | alias neighbors = neighbours; 838 | alias neighboursIn = neighbours; 839 | alias neighboursOut = neighbours; 840 | } 841 | 842 | alias neighborsIn = neighboursIn; 843 | alias neighborsOut = neighboursOut; 844 | 845 | size_t vertexCount() @property @safe const nothrow pure 846 | { 847 | return graph_.vertexCount; 848 | } 849 | 850 | size_t vertexCount(in size_t n) @property @safe pure 851 | { 852 | static if (directed) 853 | { 854 | assert(sumHead_.length == neighboursIn_.length + 1); 855 | assert(sumTail_.length == neighboursOut_.length + 1); 856 | assert(sumHead_.length == incidentEdgesIn_.length + 1); 857 | assert(sumTail_.length == incidentEdgesOut_.length + 1); 858 | } 859 | else 860 | { 861 | assert(sumTail_.length == neighbours_.length + 1); 862 | assert(sumHead_.length == incidentEdges_.length + 1); 863 | } 864 | 865 | immutable size_t l = sumTail_.length; 866 | graph_.vertexCount = n; 867 | 868 | static if (directed) 869 | { 870 | neighboursIn_.length = n; 871 | neighboursOut_.length = n; 872 | incidentEdgesIn_.length = n; 873 | incidentEdgesOut_.length = n; 874 | 875 | if (n >= l) 876 | { 877 | neighboursIn_[l - 1 .. $] = null; 878 | neighboursOut_[l - 1 .. $] = null; 879 | incidentEdgesIn_[l - 1 .. $] = null; 880 | incidentEdgesOut_[l - 1 .. $] = null; 881 | } 882 | } 883 | else 884 | { 885 | neighbours_.length = n; 886 | incidentEdges_.length = n; 887 | 888 | if (n >= l) 889 | { 890 | neighbours_[l - 1 .. $] = null; 891 | incidentEdges_[l - 1 .. $] = null; 892 | } 893 | } 894 | return vertexCount; 895 | } 896 | } 897 | 898 | unittest 899 | { 900 | import std.typetuple; 901 | 902 | foreach (Graph; TypeTuple!(IndexedEdgeList, CachedEdgeList)) 903 | { 904 | foreach (directed; TypeTuple!(true, false)) 905 | { 906 | /* We begin by creating two graphs with the different 907 | * addEdge methods and ensuring that they wind up with 908 | * the same internal data. 909 | */ 910 | auto g1 = new Graph!directed; 911 | auto g2 = new Graph!directed; 912 | g1.vertexCount = g2.vertexCount = 10; 913 | assert(g1.vertexCount == 10); 914 | assert(g2.vertexCount == 10); 915 | 916 | foreach (v; 0 .. 10) 917 | { 918 | assert(g1.isVertex(v), format("%s should be a vertex!")); 919 | } 920 | 921 | foreach (v; 10 .. 20) 922 | { 923 | assert(!g1.isVertex(v), format("%s should not be a vertex!")); 924 | } 925 | 926 | g1.addEdge(5, 8); 927 | g1.addEdge(5, 4); 928 | g1.addEdge(7, 4); 929 | g1.addEdge(3, 4); 930 | g1.addEdge(6, 9); 931 | g1.addEdge(3, 2); 932 | 933 | g2.addEdge([5, 8, 5, 4, 7, 4, 3, 4, 6, 9, 3, 2]); 934 | 935 | assert(g1.edgeCount == 6); 936 | assert(g2.edgeCount == 6); 937 | 938 | if (directed) 939 | { 940 | assert(g1.tail_ == [5, 5, 7, 3, 6, 3]); 941 | assert(g2.tail_ == [5, 5, 7, 3, 6, 3]); 942 | assert(g1.head_ == [8, 4, 4, 4, 9, 2]); 943 | assert(g2.head_ == [8, 4, 4, 4, 9, 2]); 944 | 945 | assert(g1.indexTail_ == [5, 3, 1, 0, 4, 2]); 946 | assert(g2.indexTail_ == [5, 3, 1, 0, 4, 2]); 947 | assert(g1.indexHead_ == [5, 3, 1, 2, 0, 4]); 948 | assert(g2.indexHead_ == [5, 3, 1, 2, 0, 4]); 949 | 950 | assert(g1.sumTail_ == [0, 0, 0, 0, 2, 2, 4, 5, 6, 6, 6]); 951 | assert(g2.sumTail_ == [0, 0, 0, 0, 2, 2, 4, 5, 6, 6, 6]); 952 | assert(g1.sumHead_ == [0, 0, 0, 1, 1, 4, 4, 4, 4, 5, 6]); 953 | assert(g2.sumHead_ == [0, 0, 0, 1, 1, 4, 4, 4, 4, 5, 6]); 954 | } 955 | else 956 | { 957 | assert(g1.tail_ == [5, 4, 4, 3, 6, 2]); 958 | assert(g2.tail_ == [5, 4, 4, 3, 6, 2]); 959 | assert(g1.head_ == [8, 5, 7, 4, 9, 3]); 960 | assert(g2.head_ == [8, 5, 7, 4, 9, 3]); 961 | 962 | assert(g1.indexTail_ == [5, 3, 1, 2, 0, 4]); 963 | assert(g2.indexTail_ == [5, 3, 1, 2, 0, 4]); 964 | assert(g1.indexHead_ == [5, 3, 1, 2, 0, 4]); 965 | assert(g2.indexHead_ == [5, 3, 1, 2, 0, 4]); 966 | 967 | assert(g1.sumTail_ == [0, 0, 0, 1, 2, 4, 5, 6, 6, 6, 6]); 968 | assert(g2.sumTail_ == [0, 0, 0, 1, 2, 4, 5, 6, 6, 6, 6]); 969 | assert(g1.sumHead_ == [0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 6]); 970 | assert(g2.sumHead_ == [0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 6]); 971 | } 972 | 973 | foreach (immutable v; 0 .. g1.vertexCount) 974 | { 975 | assert(g1.degreeIn(v) == g2.degreeIn(v)); 976 | assert(g1.degreeOut(v) == g2.degreeOut(v)); 977 | static if (!directed) 978 | { 979 | assert(g1.degree(v) == g2.degree(v)); 980 | } 981 | } 982 | 983 | /* If the above all passes, then we know that the internal data 984 | * representation is correct and we can focus on how the graph 985 | * transforms this into output. 986 | * 987 | * Let's start by checking that vertex IDs are recognized ... 988 | */ 989 | foreach (immutable i; -10 .. 20) 990 | { 991 | if (0 <= i && i < g1.vertexCount) 992 | { 993 | assert(g1.isVertex(i)); 994 | } 995 | else 996 | { 997 | assert(!g1.isVertex(i)); 998 | } 999 | } 1000 | 1001 | /* Now check that edges are correctly recognized and that the 1002 | * edge ID representation works. 1003 | */ 1004 | foreach (immutable h; -10 .. 20) 1005 | { 1006 | foreach (immutable t; -10 .. 20) 1007 | { 1008 | if (directed) 1009 | { 1010 | if ((h == 5 && t == 8) || 1011 | (h == 5 && t == 4) || 1012 | (h == 7 && t == 4) || 1013 | (h == 3 && t == 4) || 1014 | (h == 6 && t == 9) || 1015 | (h == 3 && t == 2)) 1016 | { 1017 | assert(g1.isEdge(h, t)); 1018 | auto i = g1.edgeID(h, t); 1019 | assert(h == g1.tail_[i]); 1020 | assert(t == g1.head_[i]); 1021 | } 1022 | else 1023 | { 1024 | assert(!g1.isEdge(h, t)); 1025 | assertThrown(g1.edgeID(h, t)); 1026 | } 1027 | } 1028 | else 1029 | { 1030 | if ((h == 5 && t == 8) || (h == 8 && t == 5) || 1031 | (h == 5 && t == 4) || (h == 4 && t == 5) || 1032 | (h == 7 && t == 4) || (h == 4 && t == 7) || 1033 | (h == 3 && t == 4) || (h == 4 && t == 3) || 1034 | (h == 6 && t == 9) || (h == 9 && t == 6) || 1035 | (h == 3 && t == 2) || (h == 2 && t == 3)) 1036 | { 1037 | assert(g1.isEdge(h, t)); 1038 | auto i = g1.edgeID(h, t); 1039 | if (h <= t) 1040 | { 1041 | assert(h == g1.tail_[i]); 1042 | assert(t == g1.head_[i]); 1043 | } 1044 | else 1045 | { 1046 | assert(h == g1.head_[i]); 1047 | assert(t == g1.tail_[i]); 1048 | } 1049 | } 1050 | else 1051 | { 1052 | assert(!g1.isEdge(h, t)); 1053 | assertThrown(g1.edgeID(h, t)); 1054 | } 1055 | } 1056 | } 1057 | } 1058 | 1059 | // Another check on edge ID. 1060 | foreach (immutable e; 0 .. g1.edgeCount) 1061 | { 1062 | size_t h = g1.tail_[e]; 1063 | size_t t = g1.head_[e]; 1064 | assert(e == g1.edgeID(h, t)); 1065 | if (!directed) 1066 | { 1067 | assert(e == g1.edgeID(t, h)); 1068 | } 1069 | } 1070 | 1071 | // Let's check the edge and neighbour functions. 1072 | foreach (immutable v; 0 .. g1.vertexCount) 1073 | { 1074 | foreach (immutable e, immutable n; zip(g1.incidentEdgesOut(v), g1.neighboursOut(v))) 1075 | { 1076 | if (directed || v <= n) 1077 | { 1078 | assert(g1.edge[e][0] == v); 1079 | assert(g1.edge[e][1] == n); 1080 | } 1081 | else 1082 | { 1083 | assert(g1.edge[e][0] == n); 1084 | assert(g1.edge[e][1] == v); 1085 | } 1086 | } 1087 | 1088 | foreach (immutable e, immutable n; zip(g1.incidentEdgesIn(v), g1.neighboursIn(v))) 1089 | { 1090 | if (directed || v > n) 1091 | { 1092 | assert(g1.edge[e][0] == n); 1093 | assert(g1.edge[e][1] == v); 1094 | } 1095 | else 1096 | { 1097 | assert(g1.edge[e][0] == v); 1098 | assert(g1.edge[e][1] == n); 1099 | } 1100 | } 1101 | 1102 | static if (!directed) 1103 | { 1104 | foreach (immutable e, immutable n; zip(g1.incidentEdges(v), g1.neighbours(v))) 1105 | { 1106 | if (v <= n) 1107 | { 1108 | assert(g1.edge[e][0] == v); 1109 | assert(g1.edge[e][1] == n); 1110 | } 1111 | else 1112 | { 1113 | assert(g1.edge[e][0] == n); 1114 | assert(g1.edge[e][1] == v); 1115 | } 1116 | } 1117 | } 1118 | } 1119 | 1120 | // Check that altering the number of vertices works OK. 1121 | g1.vertexCount = 20; 1122 | assert(g1.vertexCount == 20); 1123 | assertThrown(g1.vertexCount(5)); 1124 | g1.vertexCount = 10; 1125 | 1126 | // Check that now we've re-resized it it's back to where it was. 1127 | assert(g1.tail_ == g2.tail_); 1128 | assert(g1.head_ == g2.head_); 1129 | assert(g1.indexTail_ == g2.indexTail_); 1130 | assert(g1.indexHead_ == g2.indexHead_); 1131 | assert(g1.sumTail_ == g2.sumTail_); 1132 | assert(g1.sumHead_ == g2.sumHead_); 1133 | } 1134 | } 1135 | } 1136 | -------------------------------------------------------------------------------- /source/dgraph/metric.d: -------------------------------------------------------------------------------- 1 | module dgraph.metric; 2 | 3 | import std.algorithm, std.conv, std.range, std.traits; 4 | 5 | import dgraph.graph; 6 | 7 | /** 8 | * Simple queue implementation for internal use. This will probably be removed 9 | * once Phobos has an effective queue container. 10 | * 11 | * This is derived from Bearophile's $(LINK2 http://rosettacode.org/wiki/Queue/Usage#Faster_Version, 12 | * RosettaCode example of a circular queue), which he has kindly agreed to allow 13 | * to be $(LINK2 http://forum.dlang.org/post/mvmzvkjpbhazezlsydim@forum.dlang.org, 14 | * used under the terms of the Boost licence). 15 | */ 16 | struct VertexQueue 17 | { 18 | private size_t length_, maxLength, head, tail; 19 | private size_t[] queue; 20 | 21 | this(size_t m) 22 | { 23 | maxLength = m; 24 | queue = new size_t[maxLength]; 25 | } 26 | 27 | bool empty() @property const pure nothrow 28 | { 29 | return (length_ == 0); 30 | } 31 | 32 | size_t length() @property const pure nothrow 33 | { 34 | return length_; 35 | } 36 | 37 | void push(immutable size_t v) nothrow 38 | { 39 | assert(v < maxLength, "Vertex ID is too large!"); 40 | queue[tail] = v; 41 | tail = (tail + 1) % maxLength; 42 | ++length_; 43 | assert(length_ <= maxLength, "Length has exceeded total number of vertices!"); 44 | } 45 | 46 | auto front() @property const pure 47 | { 48 | assert(!this.empty, "Node queue is empty!"); 49 | return queue[head]; 50 | } 51 | 52 | void pop() nothrow 53 | { 54 | head = (head + 1) % maxLength; 55 | --length_; 56 | } 57 | } 58 | 59 | unittest 60 | { 61 | auto q = VertexQueue(8); 62 | q.push(3); 63 | q.push(7); 64 | q.push(4); 65 | assert(q.front == 3); 66 | q.pop(); 67 | assert(q.front == 7); 68 | q.pop(); 69 | assert(q.front == 4); 70 | q.push(5); 71 | assert(q.front == 4); 72 | q.pop(); 73 | assert(q.front == 5); 74 | q.pop(); 75 | assert(q.empty); 76 | q.push(2); 77 | assert(q.front == 2); 78 | q.pop(); 79 | assert(q.empty); 80 | } 81 | 82 | /** 83 | * Calculate betweenness centrality of vertices in a graph, using the algorithm 84 | * developed by Ulrik Brandes (2001) J. Math. Sociol. 25(2): 163-177. 85 | * 86 | * The optional function parameter ignore allows the user to indicate 87 | * individual vertices to ignore in the calculation. 88 | */ 89 | auto ref betweenness(T = double, Graph)(ref Graph g, bool[] ignore = null) 90 | if (isFloatingPoint!T && isGraph!Graph) 91 | { 92 | T[] centrality = new T[g.vertexCount]; 93 | return betweenness!(T, Graph)(g, centrality, ignore); 94 | } 95 | 96 | auto ref betweenness(T = double, Graph)(ref Graph g, ref T[] centrality, bool[] ignore = null) 97 | { 98 | centrality.length = g.vertexCount; 99 | centrality[] = to!T(0); 100 | size_t[] stack = new size_t[g.vertexCount]; 101 | T[] sigma = new T[g.vertexCount]; 102 | T[] delta = new T[g.vertexCount]; 103 | long[] d = new long[g.vertexCount]; 104 | auto q = VertexQueue(g.vertexCount); 105 | Appender!(size_t[])[] p = new Appender!(size_t[])[g.vertexCount]; 106 | 107 | sigma[] = to!T(0); 108 | delta[] = to!T(0); 109 | d[] = -1L; 110 | 111 | foreach (immutable s; 0 .. g.vertexCount) 112 | { 113 | if (ignore && ignore[s]) 114 | { 115 | continue; 116 | } 117 | 118 | size_t stackLength = 0; 119 | assert(q.empty); 120 | sigma[s] = to!T(1); 121 | d[s] = 0L; 122 | q.push(s); 123 | 124 | while (!q.empty) 125 | { 126 | size_t v = q.front; 127 | q.pop(); 128 | stack[stackLength] = v; 129 | ++stackLength; 130 | foreach (immutable w; g.neighboursOut(v)) 131 | { 132 | if (ignore && ignore[w]) 133 | { 134 | continue; 135 | } 136 | 137 | if (d[w] < 0L) 138 | { 139 | q.push(w); 140 | d[w] = d[v] + 1L; 141 | assert(sigma[w] == to!T(0)); 142 | sigma[w] = sigma[v]; 143 | p[w].clear; 144 | p[w].put(v); 145 | } 146 | else if (d[w] > (d[v] + 1L)) 147 | { 148 | /* w has already been encountered, but we've 149 | found a shorter path to the source. This 150 | is probably only relevant to the weighted 151 | case, but let's keep it here in order to 152 | be ready for that update. */ 153 | d[w] = d[v] + 1L; 154 | sigma[w] = sigma[v]; 155 | p[w].clear; 156 | p[w].put(v); 157 | } 158 | else if (d[w] == (d[v] + 1L)) 159 | { 160 | sigma[w] += sigma[v]; 161 | p[w].put(v); 162 | } 163 | } 164 | } 165 | 166 | while (stackLength > to!size_t(0)) 167 | { 168 | --stackLength; 169 | auto w = stack[stackLength]; 170 | foreach (immutable v; p[w].data) 171 | { 172 | delta[v] += ((sigma[v] / sigma[w]) * (to!T(1) + delta[w])); 173 | } 174 | if (w != s) 175 | { 176 | centrality[w] += delta[w]; 177 | } 178 | sigma[w] = to!T(0); 179 | delta[w] = to!T(0); 180 | d[w] = -1L; 181 | } 182 | } 183 | 184 | static if (!g.directed) 185 | { 186 | centrality[] /= 2; 187 | } 188 | 189 | return centrality; 190 | } 191 | 192 | /** 193 | * Calculate the size of the largest connected cluster in the graph. 194 | * 195 | * The function parameter ignore allows the user to specify individual 196 | * vertices to ignore for the purposes of the calculation. 197 | * 198 | * This algorithm is a rather ad-hoc construction inspired by Brandes' 199 | * algorithm for betweenness centrality. No claims are made for its 200 | * performance or even correctness. 201 | */ 202 | size_t largestClusterSize(Graph)(ref Graph g, bool[] ignore = null) 203 | if (isGraph!Graph) 204 | { 205 | long[] cluster = new long[g.vertexCount]; 206 | cluster[] = -1L; 207 | auto q = VertexQueue(g.vertexCount); 208 | size_t largestCluster = 0; 209 | 210 | foreach (immutable s; 0 .. g.vertexCount) 211 | { 212 | if (ignore && ignore[s]) 213 | { 214 | continue; 215 | } 216 | else if (cluster[s] < 0) 217 | { 218 | assert(q.empty); 219 | cluster[s] = 0; 220 | q.push(s); 221 | size_t clusterSize = 1; 222 | 223 | while (!q.empty) 224 | { 225 | size_t v = q.front; 226 | q.pop(); 227 | 228 | static if (g.directed) 229 | { 230 | auto allNeighbours = chain(g.neighboursIn(v), g.neighboursOut(v)); 231 | } 232 | else 233 | { 234 | auto allNeighbours = g.neighbours(v); 235 | } 236 | 237 | foreach (immutable w; allNeighbours) 238 | { 239 | if (ignore && ignore[w]) 240 | { 241 | continue; 242 | } 243 | else if (cluster[w] < 0) 244 | { 245 | q.push(w); 246 | cluster[w] = clusterSize; 247 | ++clusterSize; 248 | } 249 | } 250 | } 251 | 252 | largestCluster = max(largestCluster, clusterSize); 253 | } 254 | } 255 | 256 | return largestCluster; 257 | } 258 | 259 | unittest 260 | { 261 | import std.stdio, std.typecons; 262 | 263 | void clusterTest1(Graph)() 264 | if (isGraph!Graph) 265 | { 266 | static if (is(Graph == class)) 267 | { 268 | auto g = new Graph; 269 | } 270 | else 271 | { 272 | auto g = Graph; 273 | } 274 | bool[] ignore = new bool[5]; 275 | g.vertexCount = 5; 276 | g.addEdge(0, 1); 277 | g.addEdge(1, 2); 278 | g.addEdge(3, 4); 279 | size_t largest = largestClusterSize(g); 280 | writeln("largest cluster size = ", largest); 281 | assert(largestClusterSize(g) == largestClusterSize(g, ignore)); 282 | 283 | ignore[0 .. 2] = true; 284 | largest = largestClusterSize(g, ignore); 285 | writeln("largest cluster size = ", largest); 286 | } 287 | 288 | void clusterTest2(Graph)() 289 | if (isGraph!Graph) 290 | { 291 | static if (is(Graph == class)) 292 | { 293 | auto g = new Graph; 294 | } 295 | else 296 | { 297 | auto g = Graph; 298 | } 299 | bool[] ignore = new bool[100]; 300 | g.vertexCount = 100; 301 | foreach (immutable i; 0 .. 100) 302 | { 303 | foreach (immutable j; i .. 100) 304 | { 305 | g.addEdge(i, j); 306 | } 307 | } 308 | writeln("largest cluster size = ", largestClusterSize(g)); 309 | assert(largestClusterSize(g) == largestClusterSize(g, ignore)); 310 | foreach (immutable i; 0 .. 100) 311 | { 312 | ignore[i] = true; 313 | writeln(i, ": largest cluster size = ", largestClusterSize(g, ignore)); 314 | } 315 | } 316 | 317 | void clusterTest3(Graph)(immutable size_t n) 318 | if (isGraph!Graph) 319 | { 320 | static if (is(Graph == class)) 321 | { 322 | auto g = new Graph; 323 | } 324 | else 325 | { 326 | auto g = Graph; 327 | } 328 | bool[] ignore = new bool[n]; 329 | g.vertexCount = n; 330 | foreach (immutable i; 0 .. n - 1) 331 | { 332 | g.addEdge(i, i + 1); 333 | } 334 | writeln("largest cluster size = ", largestClusterSize(g)); 335 | assert(largestClusterSize(g) == largestClusterSize(g, ignore)); 336 | ignore[n / 4] = true; 337 | writeln("largest cluster size = ", largestClusterSize(g, ignore)); 338 | } 339 | 340 | void clusterTest50(Graph)() 341 | if (isGraph!Graph) 342 | { 343 | import std.random; 344 | import dgraph.test.samplegraph50; 345 | 346 | static if (is(Graph == class)) 347 | { 348 | auto g = new Graph; 349 | } 350 | else 351 | { 352 | auto g = Graph; 353 | } 354 | bool[] ignore = new bool[50]; 355 | g.vertexCount = 50; 356 | g.addEdge(sampleGraph50); 357 | writeln("[[50]] largest cluster size = ", largestClusterSize(g, ignore)); 358 | 359 | foreach (immutable i; 0 .. 10) 360 | { 361 | auto sample1 = randomSample(iota(50), 7, Random(100 * i * i)); 362 | ignore[] = false; 363 | foreach (immutable s; sample1) 364 | { 365 | ignore[s] = true; 366 | } 367 | writeln("[[50.", i, "]] largest cluster size = ", largestClusterSize(g, ignore)); 368 | } 369 | 370 | } 371 | 372 | clusterTest1!(CachedEdgeList!false)(); 373 | clusterTest2!(CachedEdgeList!false)(); 374 | clusterTest1!(CachedEdgeList!true)(); 375 | clusterTest2!(CachedEdgeList!true)(); 376 | 377 | clusterTest50!(CachedEdgeList!false)(); 378 | clusterTest50!(CachedEdgeList!true)(); 379 | 380 | clusterTest3!(CachedEdgeList!false)(40); 381 | clusterTest3!(CachedEdgeList!true)(40); 382 | } 383 | -------------------------------------------------------------------------------- /source/dgraph/test/samplegraph50.d: -------------------------------------------------------------------------------- 1 | // Written in the D programming language. 2 | 3 | module dgraph.test.samplegraph50; 4 | 5 | /** 6 | Sample graph for use in testing. The array represents an 7 | undirected regular graph with 50 vertices, each having 4 8 | neighbours. 9 | 10 | Each successive pair of array entries represents a single 11 | undirected link. 12 | */ 13 | immutable size_t[200] sampleGraph50 = 14 | [ 0, 24, 15 | 0, 25, 16 | 0, 16, 17 | 0, 26, 18 | 1, 37, 19 | 1, 38, 20 | 1, 11, 21 | 1, 14, 22 | 2, 19, 23 | 2, 33, 24 | 2, 35, 25 | 2, 27, 26 | 3, 40, 27 | 3, 39, 28 | 3, 23, 29 | 3, 19, 30 | 4, 18, 31 | 4, 31, 32 | 4, 37, 33 | 4, 7, 34 | 5, 17, 35 | 5, 49, 36 | 5, 18, 37 | 5, 28, 38 | 6, 27, 39 | 6, 42, 40 | 6, 9, 41 | 6, 44, 42 | 7, 19, 43 | 7, 18, 44 | 7, 49, 45 | 8, 36, 46 | 8, 43, 47 | 8, 41, 48 | 8, 33, 49 | 9, 14, 50 | 9, 11, 51 | 9, 46, 52 | 10, 26, 53 | 10, 36, 54 | 10, 35, 55 | 10, 41, 56 | 11, 32, 57 | 11, 21, 58 | 12, 21, 59 | 12, 23, 60 | 12, 29, 61 | 12, 35, 62 | 13, 45, 63 | 13, 25, 64 | 13, 38, 65 | 13, 29, 66 | 14, 16, 67 | 14, 39, 68 | 15, 48, 69 | 15, 49, 70 | 15, 44, 71 | 15, 38, 72 | 16, 47, 73 | 16, 43, 74 | 17, 25, 75 | 17, 41, 76 | 17, 49, 77 | 18, 28, 78 | 19, 40, 79 | 20, 33, 80 | 20, 21, 81 | 20, 24, 82 | 20, 31, 83 | 21, 32, 84 | 22, 32, 85 | 22, 46, 86 | 22, 34, 87 | 22, 37, 88 | 23, 30, 89 | 23, 26, 90 | 24, 43, 91 | 24, 30, 92 | 25, 42, 93 | 26, 35, 94 | 27, 39, 95 | 27, 46, 96 | 28, 47, 97 | 28, 34, 98 | 29, 33, 99 | 29, 36, 100 | 30, 38, 101 | 30, 34, 102 | 31, 48, 103 | 31, 45, 104 | 32, 46, 105 | 34, 45, 106 | 36, 48, 107 | 37, 48, 108 | 39, 40, 109 | 40, 47, 110 | 41, 44, 111 | 42, 47, 112 | 42, 44, 113 | 43, 45 114 | ]; 115 | -------------------------------------------------------------------------------- /source/dgraph/test/tests.d: -------------------------------------------------------------------------------- 1 | // Written in the D programming language. 2 | 3 | /** 4 | Test collection for dgraph library. 5 | 6 | Authors: $(LINK2 http://braingam.es/, Joseph Rushton Wakeling) 7 | Copyright: Copyright © 2013 Joseph Rushton Wakeling 8 | License: This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see $(LINK http://www.gnu.org/licenses/). 20 | */ 21 | 22 | module dgraph.test.tests; 23 | 24 | import std.algorithm, std.conv, std.exception; 25 | 26 | import dgraph.graph; 27 | 28 | /** 29 | * Tests adding edges to a graph, either one at a time or all in one go. Can 30 | * be used e.g. for benchmarking or for checking that network properties are 31 | * reliably imported. 32 | */ 33 | void testAddEdge(Graph, bool allAtOnce = false, ushort verbose = 0, T : size_t) 34 | (immutable size_t v, T[] edgeList) 35 | if (isGraph!Graph) 36 | { 37 | assert(edgeList.length % 2 == 0); 38 | static if (is(Graph == class)) 39 | { 40 | auto g = new Graph; 41 | } 42 | else 43 | { 44 | auto g = Graph; 45 | } 46 | g.vertexCount = v; 47 | 48 | static if (allAtOnce) 49 | { 50 | g.addEdge(edgeList); 51 | } 52 | else 53 | { 54 | foreach (immutable i; 0 .. edgeList.length / 2) 55 | { 56 | g.addEdge(edgeList[2*i], edgeList[2 * i + 1]); 57 | } 58 | } 59 | 60 | static if (verbose > 0) 61 | { 62 | import std.stdio; 63 | static if (g.directed) 64 | { 65 | writeln("Directed graph."); 66 | } 67 | else 68 | { 69 | writeln("Undirected graph."); 70 | } 71 | writeln("Number of vertices: ", g.vertexCount); 72 | writeln("Number of edges: ", g.edgeCount); 73 | static if (g.directed) 74 | { 75 | writeln("Incoming neighbours of vertex 0: ", g.neighboursIn(0)); 76 | writeln("Outgoing neighbours of vertex 0: ", g.neighboursOut(0)); 77 | } 78 | else 79 | { 80 | writeln("Neighbours of node 0: ", g.neighbours(0)); 81 | } 82 | } 83 | static if (verbose > 1) 84 | { 85 | static if (g.directed) 86 | { 87 | writeln("In- and out-degrees of vertices:"); 88 | foreach (immutable i; 0 .. g.vertexCount) 89 | { 90 | writeln("\t", i, "\t", g.degreeIn(i), "\t", g.degreeOut(i)); 91 | } 92 | } 93 | else 94 | { 95 | writeln("Degrees of vertices:"); 96 | foreach (immutable i; 0 .. g.vertexCount) 97 | { 98 | writeln("\t", i, "\t", g.degree(i)); 99 | } 100 | } 101 | } 102 | static if (verbose > 2) 103 | { 104 | writeln("Incoming neighbours for vertices:"); 105 | foreach (immutable i; 0 .. g.vertexCount) 106 | { 107 | write("\t", i, ": "); 108 | foreach (immutable n; g.neighboursIn(i)) 109 | { 110 | write(" ", n); 111 | } 112 | writeln; 113 | assert(isSorted(g.neighboursIn(i))); 114 | } 115 | writeln("Outgoing neighbours for vertices:"); 116 | foreach (immutable i; 0 .. g.vertexCount) 117 | { 118 | write("\t", i, ": "); 119 | foreach (immutable n; g.neighboursOut(i)) 120 | { 121 | write(" ", n); 122 | } 123 | writeln; 124 | assert(isSorted(g.neighboursOut(i))); 125 | } 126 | } 127 | } 128 | 129 | /// Tests that the edgeID function returns correct values for all edges in the graph. 130 | void testEdgeID(Graph)(ref Graph g) 131 | if (isGraph!Graph) 132 | { 133 | foreach (immutable i; 0 .. g.edgeCount) 134 | { 135 | auto edge = g.edge[i]; 136 | size_t id = g.edgeID(edge[0], edge[1]); 137 | enforce(i == id, text("Edge ID failure for edge ", i, ": edgeID(", edge[0], ", ", edge[1], ") returns ", id)); 138 | static if (!Graph.directed) 139 | { 140 | id = g.edgeID(edge[1], edge[0]); 141 | enforce(i == id, text("Edge ID failure for edge ", i, ": edgeID(", edge[1], ", ", edge[0], ") returns ", id)); 142 | } 143 | } 144 | } 145 | 146 | unittest 147 | { 148 | import std.typetuple; 149 | foreach (Graph; TypeTuple!(IndexedEdgeList, CachedEdgeList)) 150 | { 151 | foreach (directed; TypeTuple!(false, true)) 152 | { 153 | { 154 | import dgraph.test.samplegraph50; 155 | auto g = new Graph!directed; 156 | g.vertexCount = 50; 157 | g.addEdge(sampleGraph50); 158 | testEdgeID(g); 159 | } 160 | 161 | { 162 | import dgraph.test.samplegraph10k; 163 | auto g = new Graph!directed; 164 | g.vertexCount = 10_000; 165 | g.addEdge(sampleGraph10k); 166 | testEdgeID(g); 167 | } 168 | } 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /util/betweenness10k/source/betweenness10k.d: -------------------------------------------------------------------------------- 1 | import std.datetime, std.stdio; 2 | 3 | import dgraph.graph, dgraph.metric, dgraph.test.samplegraph10k; 4 | 5 | void betw(Graph)(ref Graph g) 6 | if(isGraph!Graph) 7 | { 8 | auto centrality = betweenness(g); 9 | assert(centrality.length == g.vertexCount); 10 | writeln("Centrality values:"); 11 | /* foreach (immutable i, immutable c; centrality) 12 | { 13 | writeln("\t", i, "\t", c); 14 | }*/ 15 | } 16 | 17 | void main() 18 | { 19 | alias Graph = CachedEdgeList!false; 20 | auto g = new Graph; 21 | g.vertexCount = 10_000; 22 | 23 | foreach (immutable i; 0 .. sampleGraph10k.length / 2) 24 | { 25 | g.addEdge(sampleGraph10k[2*i], sampleGraph10k[2 * i + 1]); 26 | } 27 | writeln("Vertex count: ", g.vertexCount); 28 | writeln("Edge count: ", g.edgeCount); 29 | 30 | StopWatch watch; 31 | watch.start; 32 | foreach (immutable _; 0 .. 1) 33 | { 34 | betw(g); 35 | } 36 | watch.stop; 37 | writeln("Done in ", watch.peek.msecs, " ms."); 38 | } 39 | -------------------------------------------------------------------------------- /util/betweenness50/source/betweenness50.d: -------------------------------------------------------------------------------- 1 | import std.datetime, std.stdio; 2 | 3 | import dgraph.graph, dgraph.metric, dgraph.test.samplegraph50; 4 | 5 | void betw(Graph)(ref Graph g) 6 | if(isGraph!Graph) 7 | { 8 | auto centrality = betweenness(g); 9 | assert(centrality.length == g.vertexCount); 10 | /* writeln("Centrality values:"); 11 | foreach (immutable i, immutable c; centrality) 12 | { 13 | writeln("\t", i, "\t", c); 14 | }*/ 15 | } 16 | 17 | void main() 18 | { 19 | alias Graph = CachedEdgeList!false; 20 | auto g = new Graph; 21 | g.vertexCount = 50; 22 | 23 | foreach (immutable i; 0 .. sampleGraph50.length / 2) 24 | { 25 | g.addEdge(sampleGraph50[2*i], sampleGraph50[2 * i + 1]); 26 | } 27 | writeln("Vertex count: ", g.vertexCount); 28 | writeln("Edge count: ", g.edgeCount); 29 | 30 | StopWatch watch; 31 | watch.start; 32 | foreach (immutable _; 0 .. 10_000) 33 | { 34 | betw(g); 35 | } 36 | watch.stop; 37 | writeln("Done in ", watch.peek.msecs, " ms."); 38 | } 39 | -------------------------------------------------------------------------------- /util/graphtest/source/graphtest.d: -------------------------------------------------------------------------------- 1 | // Written in the D programming language. 2 | 3 | /** 4 | Simple test file benchmarking the generation of small and large graphs by the 5 | addition of edges one at a time. 6 | 7 | Authors: $(LINK2 http://braingam.es/, Joseph Rushton Wakeling) 8 | Copyright: Copyright © 2013 Joseph Rushton Wakeling 9 | License: This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see $(LINK http://www.gnu.org/licenses/). 21 | */ 22 | 23 | import std.datetime, std.stdio, std.typetuple; 24 | import dgraph.graph, dgraph.test.tests, dgraph.test.samplegraph50, 25 | dgraph.test.samplegraph10k; 26 | 27 | void main() 28 | { 29 | writeln("Let's benchmark some simple graph creation scenarios."); 30 | 31 | foreach (G; TypeTuple!(IndexedEdgeList, CachedEdgeList)) 32 | { 33 | foreach (directed; TypeTuple!(false, true)) 34 | { 35 | alias Graph = G!directed; 36 | StopWatch watch; 37 | 38 | writeln; 39 | writeln("Graph type: ", (Graph.directed) ? "directed " : "undirected ", __traits(identifier, Graph)); 40 | writeln; 41 | 42 | writeln("First, a graph of 50 vertices, with edges added one at a time."); 43 | writeln("This is quite quick, so we'll do it 100_001 times with the last"); 44 | writeln("time being verbose."); 45 | watch.start; 46 | foreach (immutable _; 0 .. 100_000) 47 | { 48 | testAddEdge!(Graph, true, 0)(50, sampleGraph50); 49 | } 50 | testAddEdge!(Graph, true, 1)(50, sampleGraph50); 51 | watch.stop; 52 | writeln("Done in ", watch.peek.msecs, " ms."); 53 | writeln; 54 | 55 | writeln("Now let's try a much bigger graph of 10,000 vertices, again"); 56 | writeln("with each edge being added one at a time. Because it's bigger"); 57 | writeln("we'll only do it 1001 times, the last verbosely."); 58 | watch.reset; 59 | watch.start; 60 | foreach (immutable _; 0 .. 1_000) 61 | { 62 | testAddEdge!(Graph, true, 0)(10_000, sampleGraph10k); 63 | } 64 | testAddEdge!(Graph, true, 1)(10_000, sampleGraph10k); 65 | watch.stop; 66 | writeln("Done in ", watch.peek.msecs, " ms."); 67 | writeln; 68 | } 69 | } 70 | } 71 | --------------------------------------------------------------------------------