-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1974 lines (1791 loc) · 78.9 KB
/
index.html
File metadata and controls
1974 lines (1791 loc) · 78.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logarithma — Graph Algorithms Library</title>
<meta name="description" content="High-performance Python graph algorithms library. Dijkstra, A*, Bellman-Ford, Bidirectional Dijkstra, BFS, DFS and more." />
<!-- Prism.js syntax highlighting -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" />
<style>
:root {
--bg: #0d1117;
--surface: #161b22;
--surface2: #21262d;
--border: #30363d;
--accent: #58a6ff;
--accent2: #3fb950;
--accent3: #d29922;
--danger: #f85149;
--text: #e6edf3;
--muted: #8b949e;
--radius: 8px;
--nav-h: 60px;
--sidebar-w: 240px;
--max-w: 860px;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; font-size: 16px; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.7;
}
/* ── NAV ─────────────────────────────────────────────────── */
nav {
position: fixed; top: 0; left: 0; right: 0; z-index: 100;
height: var(--nav-h);
background: rgba(13,17,23,.92);
backdrop-filter: blur(12px);
border-bottom: 1px solid var(--border);
display: flex; align-items: center; justify-content: space-between;
padding: 0 2rem;
}
.nav-brand {
display: flex; align-items: center; gap: .6rem;
font-weight: 700; font-size: 1.15rem; color: var(--text);
text-decoration: none;
}
.nav-brand .logo {
width: 28px; height: 28px;
background: linear-gradient(135deg, var(--accent), var(--accent2));
border-radius: 6px;
display: flex; align-items: center; justify-content: center;
font-size: .8rem; font-weight: 900; color: #fff;
}
.nav-badge {
font-size: .72rem; padding: 2px 8px;
background: var(--surface2); border: 1px solid var(--border);
border-radius: 20px; color: var(--accent); font-weight: 600;
}
.nav-links { display: flex; gap: 1.5rem; }
.nav-links a {
color: var(--muted); text-decoration: none; font-size: .9rem;
transition: color .2s;
}
.nav-links a:hover { color: var(--text); }
.btn-github {
display: flex; align-items: center; gap: .4rem;
padding: 6px 14px; border-radius: var(--radius);
background: var(--surface2); border: 1px solid var(--border);
color: var(--text); text-decoration: none; font-size: .85rem;
transition: border-color .2s, background .2s;
}
.btn-github:hover { border-color: var(--accent); background: var(--surface); }
/* ── LAYOUT ──────────────────────────────────────────────── */
.page-wrap {
display: flex;
padding-top: var(--nav-h);
min-height: 100vh;
}
/* ── SIDEBAR ─────────────────────────────────────────────── */
aside {
width: var(--sidebar-w);
flex-shrink: 0;
position: sticky;
top: var(--nav-h);
height: calc(100vh - var(--nav-h));
overflow-y: auto;
padding: 1.5rem 1rem;
border-right: 1px solid var(--border);
font-size: .85rem;
}
aside::-webkit-scrollbar { width: 4px; }
aside::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.sidebar-group { margin-bottom: 1.4rem; }
.sidebar-label {
text-transform: uppercase; letter-spacing: .08em;
font-size: .7rem; font-weight: 700; color: var(--muted);
margin-bottom: .5rem; padding: 0 .4rem;
}
.sidebar-group a {
display: block; padding: .3rem .5rem; border-radius: 5px;
color: var(--muted); text-decoration: none; transition: color .15s, background .15s;
}
.sidebar-group a:hover,
.sidebar-group a.active { color: var(--accent); background: rgba(88,166,255,.08); }
/* ── MAIN ────────────────────────────────────────────────── */
main {
flex: 1;
max-width: var(--max-w);
padding: 3rem 2.5rem 6rem;
margin: 0 auto;
min-width: 0;
}
/* ── HERO ────────────────────────────────────────────────── */
.hero { padding: 3rem 0 2.5rem; }
.hero-eyebrow {
font-size: .8rem; font-weight: 600;
color: var(--accent2); letter-spacing: .1em;
text-transform: uppercase; margin-bottom: .8rem;
}
.hero h1 {
font-size: 2.8rem; font-weight: 800; line-height: 1.15;
background: linear-gradient(120deg, #e6edf3 30%, var(--accent));
-webkit-background-clip: text; -webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 1rem;
}
.hero p {
font-size: 1.1rem; color: var(--muted); max-width: 580px; margin-bottom: 2rem;
}
.hero-badges { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 2rem; }
.badge {
display: inline-flex; align-items: center; gap: .35rem;
padding: 4px 12px; border-radius: 20px; font-size: .78rem; font-weight: 600;
border: 1px solid var(--border);
}
.badge-blue { color: var(--accent); background: rgba(88,166,255,.1); border-color: rgba(88,166,255,.3); }
.badge-green { color: var(--accent2); background: rgba(63,185,80,.1); border-color: rgba(63,185,80,.3); }
.badge-yellow{ color: var(--accent3); background: rgba(210,153,34,.1); border-color: rgba(210,153,34,.3); }
.hero-actions { display: flex; gap: .8rem; flex-wrap: wrap; }
.btn {
padding: 10px 20px; border-radius: var(--radius);
font-size: .9rem; font-weight: 600; text-decoration: none;
transition: opacity .2s, transform .15s;
display: inline-block;
}
.btn:hover { opacity: .88; transform: translateY(-1px); }
.btn-primary { background: var(--accent); color: #0d1117; }
.btn-secondary {
background: var(--surface2); border: 1px solid var(--border); color: var(--text);
}
/* ── SECTIONS ────────────────────────────────────────────── */
section { margin-bottom: 4rem; scroll-margin-top: calc(var(--nav-h) + 1rem); }
h2 {
font-size: 1.6rem; font-weight: 700; margin-bottom: 1rem;
padding-bottom: .5rem; border-bottom: 1px solid var(--border);
}
h3 { font-size: 1.1rem; font-weight: 700; margin: 1.8rem 0 .6rem; color: var(--text); }
h4 { font-size: .95rem; font-weight: 600; margin: 1.2rem 0 .4rem; color: var(--muted); }
p { margin-bottom: .8rem; color: var(--muted); }
p strong { color: var(--text); }
a { color: var(--accent); }
ul { padding-left: 1.4rem; color: var(--muted); }
li { margin-bottom: .25rem; }
li strong { color: var(--text); }
code {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
font-size: .85em;
background: var(--surface2); border: 1px solid var(--border);
padding: 1px 6px; border-radius: 4px; color: var(--accent);
}
/* ── CODE BLOCKS ─────────────────────────────────────────── */
.code-block { margin: 1.2rem 0; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); }
.code-header {
display: flex; align-items: center; justify-content: space-between;
padding: .5rem 1rem; background: var(--surface2);
border-bottom: 1px solid var(--border); font-size: .78rem; color: var(--muted);
}
.code-lang { color: var(--accent); font-weight: 600; }
.copy-btn {
background: none; border: 1px solid var(--border); color: var(--muted);
padding: 2px 10px; border-radius: 5px; cursor: pointer; font-size: .75rem;
transition: color .2s, border-color .2s;
}
.copy-btn:hover { color: var(--text); border-color: var(--accent); }
pre[class*="language-"] {
margin: 0 !important;
border-radius: 0 !important;
background: #161b22 !important;
font-size: .83rem !important;
}
/* ── INSTALL BOX ─────────────────────────────────────────── */
.install-box {
display: flex; align-items: center; gap: 1rem;
padding: 1rem 1.4rem; background: var(--surface);
border: 1px solid var(--border); border-radius: var(--radius);
font-family: monospace; font-size: 1rem; color: var(--accent2);
margin: 1.2rem 0;
}
.install-box .prompt { color: var(--muted); user-select: none; }
/* ── ALGO CARDS ──────────────────────────────────────────── */
.algo-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin: 1.2rem 0; }
.algo-card {
padding: 1.1rem 1.2rem;
background: var(--surface); border: 1px solid var(--border);
border-radius: var(--radius); transition: border-color .2s;
}
.algo-card:hover { border-color: var(--accent); }
.algo-card-name { font-weight: 700; font-size: .95rem; margin-bottom: .3rem; }
.algo-card-complexity {
font-family: monospace; font-size: .78rem;
color: var(--accent3); margin-bottom: .5rem;
}
.algo-card-desc { font-size: .82rem; color: var(--muted); }
.algo-card-usecase {
font-size: .78rem; color: var(--accent2); margin-top: .4rem;
padding-top: .4rem; border-top: 1px solid var(--border);
}
/* ── CALLOUT ─────────────────────────────────────────────── */
.callout {
display: flex; gap: .8rem;
padding: 1rem 1.2rem; border-radius: var(--radius);
margin: 1.2rem 0; font-size: .88rem;
}
.callout-info { background: rgba(88,166,255,.08); border: 1px solid rgba(88,166,255,.25); }
.callout-warn { background: rgba(210,153,34,.08); border: 1px solid rgba(210,153,34,.25); }
.callout-danger{ background: rgba(248,81,73,.08); border: 1px solid rgba(248,81,73,.25); }
.callout-icon { flex-shrink: 0; font-size: 1rem; margin-top: .05rem; }
.callout-body { color: var(--muted); }
.callout-body strong { color: var(--text); }
/* ── TABLE ───────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; margin: 1.2rem 0; }
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
th { text-align: left; padding: .6rem .9rem; color: var(--muted); font-weight: 600; border-bottom: 2px solid var(--border); }
td { padding: .55rem .9rem; border-bottom: 1px solid var(--border); color: var(--muted); }
td strong, td code { color: var(--text); }
tr:hover td { background: rgba(255,255,255,.02); }
/* ── VERSION TIMELINE ────────────────────────────────────── */
.timeline { margin: 1.2rem 0; }
.timeline-item { display: flex; gap: 1rem; margin-bottom: 1.5rem; }
.timeline-dot-col { display: flex; flex-direction: column; align-items: center; }
.timeline-dot {
width: 12px; height: 12px; border-radius: 50%;
background: var(--border); flex-shrink: 0; margin-top: .35rem;
}
.timeline-dot.done { background: var(--accent2); }
.timeline-dot.current { background: var(--accent); box-shadow: 0 0 0 4px rgba(88,166,255,.2); }
.timeline-dot.future { background: var(--border); }
.timeline-line { width: 1px; background: var(--border); flex: 1; margin-top: 4px; }
.timeline-content { padding-bottom: .5rem; }
.timeline-version { font-weight: 700; font-size: .9rem; margin-bottom: .2rem; }
.timeline-date { font-size: .75rem; color: var(--muted); margin-bottom: .4rem; }
.timeline-items { font-size: .83rem; color: var(--muted); }
.timeline-items li { margin-bottom: .15rem; }
/* ── FOOTER ──────────────────────────────────────────────── */
footer {
border-top: 1px solid var(--border);
padding: 2rem; text-align: center;
font-size: .82rem; color: var(--muted);
}
footer a { color: var(--accent); text-decoration: none; }
/* ── RESPONSIVE ──────────────────────────────────────────── */
@media (max-width: 768px) {
aside { display: none; }
main { padding: 2rem 1.2rem 4rem; }
.hero h1 { font-size: 2rem; }
.algo-grid { grid-template-columns: 1fr; }
.nav-links { display: none; }
}
</style>
</head>
<body>
<!-- ── NAVBAR ─────────────────────────────────────────────────── -->
<nav>
<a class="nav-brand" href="#">
<div class="logo">λ</div>
Logarithma
<span class="nav-badge">v0.6.0</span>
</a>
<div class="nav-links">
<a href="#installation">Installation</a>
<a href="#quickstart">Quick Start</a>
<a href="#algorithms">Algorithms</a>
<a href="#floyd-warshall">APSP</a>
<a href="#breaking-barrier">Breaking Barrier</a>
<a href="#mst">MST</a>
<a href="#network-flow">Flow</a>
<a href="#changelog">Changelog</a>
</div>
<a class="btn-github" href="https://github.com/softdevcan/logarithma" target="_blank">
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"/>
</svg>
GitHub
</a>
</nav>
<!-- ── PAGE WRAP ──────────────────────────────────────────────── -->
<div class="page-wrap">
<!-- ── SIDEBAR ─────────────────────────────────────────────── -->
<aside>
<div class="sidebar-group">
<div class="sidebar-label">Getting Started</div>
<a href="#intro">Introduction</a>
<a href="#installation">Installation</a>
<a href="#quickstart">Quick Start</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">Shortest Path</div>
<a href="#dijkstra">Dijkstra</a>
<a href="#astar">A*</a>
<a href="#bellman-ford">Bellman-Ford</a>
<a href="#bidir">Bidirectional Dijkstra</a>
<a href="#floyd-warshall">Floyd-Warshall</a>
<a href="#johnson">Johnson's</a>
<a href="#breaking-barrier">Breaking Barrier</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">Graph Traversal</div>
<a href="#bfs">BFS</a>
<a href="#dfs">DFS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">MST</div>
<a href="#mst">Kruskal & Prim</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">Network Flow</div>
<a href="#network-flow">Max Flow</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">Graph Properties</div>
<a href="#graph-properties">SCC & Topo Sort</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">Utilities</div>
<a href="#utils">Utils Module</a>
</div>
<div class="sidebar-group">
<div class="sidebar-label">Project</div>
<a href="#complexity">Complexity Table</a>
<a href="#changelog">Changelog</a>
<a href="#roadmap">Roadmap</a>
</div>
</aside>
<!-- ── MAIN ────────────────────────────────────────────────── -->
<main>
<!-- HERO -->
<section class="hero" id="intro">
<div class="hero-eyebrow">Python Graph Algorithms Library</div>
<h1>Logarithma</h1>
<p>
High-performance graph algorithms in Python — from classic Dijkstra to the
cutting-edge <strong>Breaking the Sorting Barrier</strong> SSSP (Duan et al., 2025).
Built on NetworkX. Production-ready, fully typed, comprehensively tested.
</p>
<div class="hero-badges">
<span class="badge badge-blue">v0.6.0</span>
<span class="badge badge-green">Python 3.8+</span>
<span class="badge badge-yellow">MIT License</span>
<span class="badge badge-blue">339 Tests</span>
<span class="badge badge-green">Cython Accelerated</span>
</div>
<div class="hero-actions">
<a href="#installation" class="btn btn-primary">Get Started</a>
<a href="https://github.com/softdevcan/logarithma" class="btn btn-secondary" target="_blank">View on GitHub</a>
</div>
</section>
<!-- INSTALLATION -->
<section id="installation">
<h2>Installation</h2>
<p>Install the latest stable version from PyPI:</p>
<div class="install-box">
<span class="prompt">$</span>
pip install logarithma
</div>
<p>To include visualization support (Matplotlib + Plotly):</p>
<div class="install-box">
<span class="prompt">$</span>
pip install logarithma[viz]
</div>
<p><strong>Requirements:</strong> Python 3.8+, NumPy ≥ 1.20, NetworkX ≥ 2.6</p>
<h3>Install from Source</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">bash</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-bash">git clone https://github.com/softdevcan/logarithma.git
cd logarithma
pip install -e ".[dev,viz]"</code></pre>
</div>
</section>
<!-- QUICK START -->
<section id="quickstart">
<h2>Quick Start</h2>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">import logarithma as lg
import networkx as nx
# Build a weighted graph
G = nx.Graph()
G.add_edge('A', 'B', weight=4)
G.add_edge('A', 'C', weight=2)
G.add_edge('B', 'C', weight=1)
G.add_edge('B', 'D', weight=5)
G.add_edge('C', 'D', weight=8)
# Dijkstra — shortest distances from A
distances = lg.dijkstra(G, 'A')
print(distances)
# {'A': 0, 'B': 3, 'C': 2, 'D': 8}
# A* — point-to-point with heuristic
result = lg.astar(G, 'A', 'D')
print(result['distance']) # 8
print(result['path']) # ['A', 'C', 'B', 'D']
# Bellman-Ford — supports negative weights
DG = nx.DiGraph()
DG.add_edge('X', 'Y', weight=4)
DG.add_edge('Y', 'Z', weight=-2)
result = lg.bellman_ford(DG, 'X')
print(result['distances']) # {'X': 0, 'Y': 4, 'Z': 2}
# BFS traversal
visited = lg.bfs(G, 'A')
print(visited) # {'A': 0, 'B': 1, 'C': 1, 'D': 2}
# Kruskal MST
mst = lg.kruskal_mst(G)
print(mst['total_weight']) # minimum spanning tree weight
# Max Flow
DG = nx.DiGraph()
DG.add_edge('S', 'A', capacity=10)
DG.add_edge('A', 'T', capacity=10)
flow = lg.max_flow(DG, 'S', 'T')
print(flow['max_flow_value']) # 10
# Tarjan SCC
DG2 = nx.DiGraph([('A','B'), ('B','C'), ('C','A')])
sccs = lg.tarjan_scc(DG2)
print(sccs) # [['A', 'B', 'C']]
# Floyd-Warshall — all-pairs shortest paths
result = lg.floyd_warshall(G)
print(result['distances']['A']['D']) # shortest distance A→D
# Johnson's — all-pairs for sparse graphs
result = lg.johnson(G)
print(result['distances']['A']['D']) # same result, faster on sparse graphs</code></pre>
</div>
</section>
<!-- ALGORITHMS OVERVIEW -->
<section id="algorithms">
<h2>Algorithms</h2>
<div class="algo-grid">
<div class="algo-card">
<div class="algo-card-name">Dijkstra</div>
<div class="algo-card-complexity">O(E + V log V)</div>
<div class="algo-card-desc">Classic single-source shortest path. Non-negative weights. Directed & undirected.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> GPS navigation, network routing, weighted graph distance queries.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">A* (A-Star)</div>
<div class="algo-card-complexity">O(b^d) — heuristic-guided</div>
<div class="algo-card-desc">Faster point-to-point with Euclidean, Manhattan or Haversine heuristic.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Game pathfinding, robot navigation, map routing with coordinates.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Bellman-Ford</div>
<div class="algo-card-complexity">O(V · E)</div>
<div class="algo-card-desc">Negative-weight edges. Negative-cycle detection with full cycle info.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Currency arbitrage detection, financial modeling, distributed routing (RIP).</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Bidirectional Dijkstra</div>
<div class="algo-card-complexity">O(E + V log V) — ~2× faster</div>
<div class="algo-card-desc">Simultaneous forward/backward search. Ideal for large point-to-point queries.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Long-distance routing, social network distance, large road networks.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Floyd-Warshall</div>
<div class="algo-card-complexity">O(V³)</div>
<div class="algo-card-desc">All-pairs shortest paths via dynamic programming. Supports negative weights.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Dense graphs, transitive closure, graph diameter, distance matrices.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Johnson's</div>
<div class="algo-card-complexity">O(V² log V + VE)</div>
<div class="algo-card-desc">All-pairs shortest paths optimized for sparse graphs. Bellman-Ford + Dijkstra.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Sparse graphs with negative weights, better than Floyd-Warshall when E ≪ V².</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Breaking Barrier SSSP</div>
<div class="algo-card-complexity">O(m log²/³ n) — beyond sorting barrier</div>
<div class="algo-card-desc">First Python impl. of Duan et al. 2025. Directed SSSP faster than Dijkstra asymptotically.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Research, large-scale directed sparse graphs, benchmarking against Dijkstra.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">BFS</div>
<div class="algo-card-complexity">O(V + E)</div>
<div class="algo-card-desc">Level-order traversal. Optimal for unweighted shortest paths.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Social network degrees of separation, web crawling, shortest hop-count.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">DFS</div>
<div class="algo-card-complexity">O(V + E)</div>
<div class="algo-card-desc">Depth-first traversal. Cycle detection. Recursive & iterative modes.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Maze solving, topological ordering, cycle detection, backtracking problems.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Kruskal MST</div>
<div class="algo-card-complexity">O(E log E)</div>
<div class="algo-card-desc">Minimum Spanning Tree via Union-Find. Undirected graphs.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Network cable layout, clustering, circuit design, minimum-cost connectivity.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Prim MST</div>
<div class="algo-card-complexity">O(E + V log V)</div>
<div class="algo-card-desc">MST via greedy min-heap expansion. Undirected graphs.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Dense graphs where Kruskal is slower, real-time MST construction.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Max Flow (Edmonds-Karp)</div>
<div class="algo-card-complexity">O(V · E²)</div>
<div class="algo-card-desc">Maximum flow via BFS-based augmenting paths. Directed capacity graphs.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Supply chain optimization, bipartite matching, image segmentation.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Tarjan SCC</div>
<div class="algo-card-complexity">O(V + E)</div>
<div class="algo-card-desc">Strongly connected components. Iterative DFS-based implementation.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Deadlock detection, compiler optimization, social network analysis.</div>
</div>
<div class="algo-card">
<div class="algo-card-name">Topological Sort</div>
<div class="algo-card-complexity">O(V + E)</div>
<div class="algo-card-desc">Linear ordering of DAG vertices. DFS and Kahn's algorithm support.</div>
<div class="algo-card-usecase"><strong>Use for:</strong> Build systems (make/npm), task scheduling, course prerequisite planning.</div>
</div>
</div>
</section>
<!-- DIJKSTRA -->
<section id="dijkstra">
<h2>Dijkstra</h2>
<p>
Classic single-source shortest path algorithm for graphs with
<strong>non-negative edge weights</strong>. Works on both directed and undirected graphs.
</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> Your default choice for shortest-path queries when all
edge weights are non-negative. Ideal for <strong>GPS navigation</strong>,
<strong>network routing protocols (OSPF)</strong>, and <strong>weighted graph distance queries</strong>.
If you have coordinates and a single target, consider A* instead.
If you need all-pairs distances, use Floyd-Warshall or Johnson's.
</div>
</div>
<h3>dijkstra(graph, source)</h3>
<p>Returns a dictionary of shortest distances from <code>source</code> to all vertices.</p>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">import logarithma as lg
import networkx as nx
G = nx.Graph()
G.add_edge('A', 'B', weight=4)
G.add_edge('A', 'C', weight=2)
G.add_edge('B', 'C', weight=1)
G.add_edge('B', 'D', weight=5)
distances = lg.dijkstra(G, 'A')
# {'A': 0, 'C': 2, 'B': 3, 'D': 8}
# Works with directed graphs too
DG = nx.DiGraph()
DG.add_edge('A', 'B', weight=1)
DG.add_edge('B', 'C', weight=2)
distances = lg.dijkstra(DG, 'A')
# {'A': 0, 'B': 1, 'C': 3}</code></pre>
</div>
<h3>dijkstra_with_path(graph, source, target=None)</h3>
<p>Returns both distances and the actual paths.</p>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">result = lg.dijkstra_with_path(G, 'A', 'D')
print(result['distances']['D']) # 8
print(result['paths']['D']) # ['A', 'C', 'B', 'D']
# All paths from source
result = lg.dijkstra_with_path(G, 'A')
for node, path in result['paths'].items():
print(f"{node}: {path}")</code></pre>
</div>
</section>
<!-- A* -->
<section id="astar">
<h2>A* (A-Star)</h2>
<p>
Heuristic-guided shortest path algorithm. Uses an admissible heuristic
<code>h(n)</code> to direct the search toward the goal, expanding far fewer nodes
than Dijkstra for point-to-point queries.
</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> Best for <strong>point-to-point</strong> queries where you have
spatial coordinates (2D/3D positions, lat/lon).
Use cases: <strong>game pathfinding</strong> (NPCs, tiles), <strong>robot navigation</strong>,
<strong>map routing</strong> with known positions. Falls back to Dijkstra when no heuristic is given.
</div>
</div>
<div class="callout callout-info">
<div class="callout-icon">ℹ️</div>
<div class="callout-body">
<strong>Optimality guarantee:</strong> A* finds the optimal path if and only if
the heuristic is <em>admissible</em> — it never overestimates the true distance
to the goal. All built-in heuristics satisfy this condition.
</div>
</div>
<h3>astar(graph, source, target, heuristic=None)</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import astar, euclidean_heuristic, manhattan_heuristic
# ── Euclidean heuristic (2D coordinate graphs) ──────────────────
G = nx.Graph()
pos = {'A': (0, 0), 'B': (3, 0), 'C': (3, 4), 'D': (6, 4)}
G.add_edge('A', 'B', weight=3)
G.add_edge('B', 'C', weight=4)
G.add_edge('A', 'C', weight=10)
G.add_edge('C', 'D', weight=3)
h = euclidean_heuristic(pos)
result = astar(G, 'A', 'D', heuristic=h)
print(result['distance']) # 10
print(result['path']) # ['A', 'B', 'C', 'D']
# ── Manhattan heuristic (grid graphs) ──────────────────────────
G_grid = nx.grid_2d_graph(5, 5)
for u, v in G_grid.edges():
G_grid[u][v]['weight'] = 1
pos_grid = {node: (node[1], node[0]) for node in G_grid.nodes()}
h_manhattan = manhattan_heuristic(pos_grid)
result = astar(G_grid, (0, 0), (4, 4), heuristic=h_manhattan)
print(result['distance']) # 8
print(result['path']) # [(0,0), (0,1), ..., (4,4)]
# ── No heuristic → equivalent to Dijkstra ──────────────────────
result = astar(G, 'A', 'D') # heuristic defaults to zero</code></pre>
</div>
<h3>Haversine — Geographic Graphs</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import astar, haversine_heuristic
# lat/lon coordinates
cities = {
'Istanbul': (41.008, 28.978),
'Ankara': (39.933, 32.859),
'Izmir': (38.419, 27.128),
}
G = nx.Graph()
G.add_edge('Istanbul', 'Ankara', weight=450) # km
G.add_edge('Ankara', 'Izmir', weight=590)
G.add_edge('Istanbul', 'Izmir', weight=480)
h = haversine_heuristic(cities)
result = astar(G, 'Istanbul', 'Izmir', heuristic=h)
print(result['distance']) # 480 (direct is cheaper)
print(result['path']) # ['Istanbul', 'Izmir']</code></pre>
</div>
<h3>astar_with_stats — Heuristic Analysis</h3>
<p>Compares how many nodes different heuristics expand:</p>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import astar_with_stats, euclidean_heuristic, zero_heuristic
# Zero heuristic (= Dijkstra)
r_zero = astar_with_stats(G_grid, (0,0), (4,4), heuristic=zero_heuristic)
print(r_zero['nodes_expanded']) # e.g. 25 — visits all nodes
# Euclidean heuristic
r_euc = astar_with_stats(G_grid, (0,0), (4,4), heuristic=euclidean_heuristic(pos_grid))
print(r_euc['nodes_expanded']) # e.g. 9 — much fewer nodes visited</code></pre>
</div>
</section>
<!-- BELLMAN-FORD -->
<section id="bellman-ford">
<h2>Bellman-Ford</h2>
<p>
The only SSSP algorithm that handles <strong>negative-weight edges</strong>.
Also detects negative-weight cycles reachable from the source.
</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> When your graph has <strong>negative edge weights</strong>
(Dijkstra cannot handle these). Key use cases:
<strong>currency arbitrage detection</strong> (negative log-exchange cycles),
<strong>distributed routing protocols</strong> (RIP), and <strong>financial cost modeling</strong>
where costs can decrease along edges.
</div>
</div>
<div class="callout callout-warn">
<div class="callout-icon">⚠️</div>
<div class="callout-body">
<strong>Directed graphs only for negative weights.</strong>
An undirected graph with any negative-weight edge always contains a negative
cycle (the edge itself forms a 2-cycle). Use <code>nx.DiGraph()</code> when
working with negative weights.
</div>
</div>
<h3>bellman_ford(graph, source)</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import bellman_ford, NegativeCycleError
import networkx as nx
# ── Graph with negative edge (no cycle) ────────────────────────
G = nx.DiGraph()
G.add_edge('A', 'B', weight=4)
G.add_edge('B', 'C', weight=-3) # negative but no cycle
G.add_edge('A', 'C', weight=5)
G.add_edge('C', 'D', weight=1)
result = bellman_ford(G, 'A')
print(result['distances'])
# {'A': 0, 'B': 4, 'C': 1, 'D': 2}
# A→B=4, then B→C: 4+(-3)=1, C→D: 1+1=2
# ── Negative cycle detection ────────────────────────────────────
G_cycle = nx.DiGraph()
G_cycle.add_edge('A', 'B', weight=1)
G_cycle.add_edge('B', 'C', weight=-3)
G_cycle.add_edge('C', 'A', weight=1) # total cycle weight: -1
try:
bellman_ford(G_cycle, 'A')
except NegativeCycleError as e:
print("Negative cycle detected!")
print("Cycle:", e.cycle) # ['A', 'B', 'C', 'A']</code></pre>
</div>
<h3>bellman_ford_path(graph, source, target)</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import bellman_ford_path
result = bellman_ford_path(G, 'A', 'D')
print(result['distance']) # 2
print(result['path']) # ['A', 'B', 'C', 'D']
# Arbitrage detection example
exchange = nx.DiGraph()
# Negative log-rates as weights: buy low, sell high
exchange.add_edge('USD', 'EUR', weight=-0.05)
exchange.add_edge('EUR', 'GBP', weight=-0.12)
exchange.add_edge('GBP', 'USD', weight=-0.04)
try:
bellman_ford(exchange, 'USD')
except NegativeCycleError:
print("Arbitrage opportunity detected!")</code></pre>
</div>
</section>
<!-- BIDIRECTIONAL DIJKSTRA -->
<section id="bidir">
<h2>Bidirectional Dijkstra</h2>
<p>
Runs two simultaneous Dijkstra searches — forward from <code>source</code> and
backward from <code>target</code> — and terminates when the frontiers meet.
Typically <strong>~2× fewer node expansions</strong> than standard Dijkstra.
</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> When you need a single source-to-target distance on
a <strong>large graph</strong> and don't have coordinate-based heuristics for A*.
Ideal for <strong>road network routing</strong>, <strong>social network "degrees of separation"</strong>,
and any <strong>large-scale point-to-point query</strong> where Dijkstra alone is too slow.
</div>
</div>
<h3>bidirectional_dijkstra(graph, source, target)</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import bidirectional_dijkstra
import networkx as nx
# ── Large road network ─────────────────────────────────────────
G = nx.grid_2d_graph(100, 100) # 10,000 nodes
for u, v in G.edges():
G[u][v]['weight'] = 1
result = bidirectional_dijkstra(G, (0, 0), (99, 99))
print(result['distance']) # 198
print(result['path'][:4]) # [(0,0), (0,1), (0,2), ...]
# ── Directed graph ─────────────────────────────────────────────
DG = nx.DiGraph()
DG.add_edge('S', 'A', weight=1)
DG.add_edge('A', 'B', weight=2)
DG.add_edge('S', 'B', weight=10) # longer direct path
DG.add_edge('B', 'T', weight=1)
result = bidirectional_dijkstra(DG, 'S', 'T')
print(result['distance']) # 4 (S→A→B→T)
print(result['path']) # ['S', 'A', 'B', 'T']
# ── Unreachable target ─────────────────────────────────────────
result = bidirectional_dijkstra(DG, 'T', 'S') # no reverse path
print(result['distance']) # inf
print(result['path']) # []</code></pre>
</div>
</section>
<!-- BFS -->
<section id="bfs">
<h2>BFS — Breadth-First Search</h2>
<p>
Level-order traversal. Finds <strong>shortest paths by edge count</strong>
in unweighted graphs.
</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> Unweighted graphs where all edges have equal cost.
Use cases: <strong>social network degrees of separation</strong>,
<strong>web crawling</strong> (pages by link depth), <strong>shortest hop-count</strong>
in network topology, and <strong>flood fill</strong> algorithms.
</div>
</div>
<h3>bfs(graph, source)</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import bfs, bfs_path
import networkx as nx
G = nx.Graph()
G.add_edges_from([('A','B'), ('B','C'), ('A','D'), ('D','E')])
# Distances (hop count) from A
distances = bfs(G, 'A')
# {'A': 0, 'B': 1, 'D': 1, 'C': 2, 'E': 2}
# Path to a target
result = bfs_path(G, 'A', 'E')
print(result['distances']['E']) # 2
print(result['paths']['E']) # ['A', 'D', 'E']</code></pre>
</div>
</section>
<!-- DFS -->
<section id="dfs">
<h2>DFS — Depth-First Search</h2>
<p>Explores as deep as possible before backtracking. Supports recursive and iterative modes.</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> When you need to explore or classify graph structure
rather than find shortest paths. Use cases: <strong>cycle detection</strong>,
<strong>maze solving</strong>, <strong>topological preprocessing</strong>,
<strong>connected component discovery</strong>, and <strong>backtracking search</strong> (puzzles, CSPs).
</div>
</div>
<h3>dfs(graph, source) / detect_cycle(graph)</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code class="language-python">from logarithma import dfs, dfs_path, detect_cycle
import networkx as nx
G = nx.Graph()
G.add_edges_from([('A','B'), ('B','C'), ('C','D'), ('A','D')])
# DFS traversal order
visited = dfs(G, 'A')
# e.g. ['A', 'B', 'C', 'D']
# Iterative mode
visited = dfs(G, 'A', mode='iterative')
# Find any path (not necessarily shortest)
path = dfs_path(G, 'A', 'D')
# ['A', 'D'] (direct edge found first)
# Cycle detection
has_cycle, cycle = detect_cycle(G)
print(has_cycle) # True (A-B-C-D-A)
print(cycle) # ['A', 'B', 'C', 'D', 'A']
# Directed graph
DG = nx.DiGraph()
DG.add_edges_from([('A','B'), ('B','C')])
has_cycle, cycle = detect_cycle(DG)
print(has_cycle) # False</code></pre>
</div>
</section>
<!-- MST -->
<section id="mst">
<h2>MST — Minimum Spanning Tree</h2>
<p>
Both algorithms find a minimum spanning tree of an <strong>undirected weighted graph</strong>.
Passing a directed graph raises <code>UndirectedGraphRequiredError</code>.
</p>
<div class="callout callout-info">
<div class="callout-icon">🎯</div>
<div class="callout-body">
<strong>When to use:</strong> When you need to connect all nodes at minimum total cost.
Use cases: <strong>network cable/pipeline layout</strong>,
<strong>clustering</strong> (remove heaviest MST edges),
<strong>circuit design</strong>, <strong>approximation algorithms</strong> for TSP.
Kruskal is better for sparse graphs; Prim for dense graphs.
</div>
</div>
<h3>kruskal_mst(graph)</h3>
<p>Union-Find based greedy approach. Sorts all edges and adds the cheapest that doesn't form a cycle.</p>
<div class="code-block">
<div class="code-header">
<span class="code-lang">python</span>