-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_creation.gleam
More file actions
167 lines (143 loc) · 4.57 KB
/
graph_creation.gleam
File metadata and controls
167 lines (143 loc) · 4.57 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
import gleam/int
import gleam/io
import yog
import yog/builder/labeled
import yog/model
pub fn main() {
io.println("=== Graph Creation Methods ===\n")
// Method 1: Builder pattern with add_nodes and add_edges
io.println("1. Builder Pattern (most flexible)")
let assert Ok(graph1) =
yog.directed()
|> yog.add_node(1, "Node A")
|> yog.add_node(2, "Node B")
|> yog.add_node(3, "Node C")
|> yog.add_edges([#(1, 2, 10), #(2, 3, 5)])
io.println(
" Built graph with "
<> int.to_string(count_nodes(graph1))
<> " nodes using builder pattern",
)
// Method 2: From edge list
io.println("\n2. From Edge List (quick and concise)")
let graph2 =
yog.from_edges(model.Directed, [#(1, 2, 10), #(2, 3, 5), #(1, 3, 15)])
io.println(
" Created graph with "
<> int.to_string(count_nodes(graph2))
<> " nodes from edge list",
)
// Method 3: From unweighted edges
io.println("\n3. From Unweighted Edges (no weights needed)")
let graph3 =
yog.from_unweighted_edges(model.Directed, [#(1, 2), #(2, 3), #(3, 4)])
io.println(
" Created unweighted graph with "
<> int.to_string(count_nodes(graph3))
<> " nodes",
)
// Method 4: From adjacency list
io.println("\n4. From Adjacency List (natural representation)")
let graph4 =
yog.from_adjacency_list(model.Directed, [
#(1, [#(2, 10), #(3, 5)]),
#(2, [#(3, 3)]),
])
io.println(
" Created graph with "
<> int.to_string(count_nodes(graph4))
<> " nodes from adjacency list",
)
// Method 5: Simple edges (weight = 1)
io.println("\n5. Simple Edges (default weight 1)")
let assert Ok(graph5) =
yog.directed()
|> yog.add_node(1, Nil)
|> yog.add_node(2, Nil)
|> yog.add_node(3, Nil)
|> yog.add_unweighted_edges([#(1, 2), #(2, 3)])
io.println(
" Created graph with "
<> int.to_string(count_nodes(graph5))
<> " nodes using simple edges",
)
// Method 6: Labeled builder (string labels)
io.println("\n6. Labeled Builder (use strings as node IDs)")
let builder =
labeled.directed()
|> labeled.add_edge(from: "home", to: "work", with: 10)
|> labeled.add_edge(from: "work", to: "gym", with: 5)
|> labeled.add_edge(from: "home", to: "gym", with: 15)
let graph6 = labeled.to_graph(builder)
io.println(
" Created labeled graph with "
<> int.to_string(count_nodes(graph6))
<> " nodes",
)
// Method 7: Labeled from list
io.println("\n7. Labeled From List (bulk creation with labels)")
let builder2 =
labeled.from_list(model.Directed, [
#("A", "B", 10),
#("B", "C", 5),
#("C", "D", 3),
])
let graph7 = labeled.to_graph(builder2)
io.println(
" Created labeled graph with "
<> int.to_string(count_nodes(graph7))
<> " nodes from list",
)
// Method 8: Labeled unweighted from list
io.println("\n8. Labeled Unweighted From List")
let builder3 =
labeled.from_unweighted_list(model.Directed, [
#("start", "middle"),
#("middle", "end"),
])
let graph8 = labeled.to_graph(builder3)
io.println(
" Created unweighted labeled graph with "
<> int.to_string(count_nodes(graph8))
<> " nodes",
)
// Method 9: Labeled simple edges
io.println("\n9. Labeled Simple Edges (labels + weight 1)")
let builder4 =
labeled.directed()
|> labeled.add_simple_edge("Alice", "Bob")
|> labeled.add_simple_edge("Bob", "Carol")
|> labeled.add_simple_edge("Carol", "Dave")
let graph9 = labeled.to_graph(builder4)
io.println(
" Created simple labeled graph with "
<> int.to_string(count_nodes(graph9))
<> " nodes",
)
// Method 10: Undirected graphs
io.println("\n10. Undirected Graphs (all methods work)")
let graph10 = yog.from_edges(model.Undirected, [#(1, 2, 5)])
io.println(
" Created undirected graph (edges work both ways) with "
<> int.to_string(count_nodes(graph10))
<> " nodes",
)
io.println("\n=== Summary ===")
io.println("• Builder pattern: Most flexible, good for complex graphs")
io.println("• from_edges: Quick creation from edge list")
io.println("• from_adjacency_list: Natural for adjacency list data")
io.println("• Labeled builder: Use strings/any type as node identifiers")
io.println("• from_list variants: Bulk creation with labels")
io.println("• Simple/unweighted: Convenient for unit weights or no weights")
}
fn count_nodes(graph: yog.Graph(n, e)) -> Int {
yog.all_nodes(graph)
|> fn(nodes) { nodes }
|> fn(nodes) { count_list(nodes) }
}
fn count_list(list: List(a)) -> Int {
case list {
[] -> 0
[_, ..rest] -> 1 + count_list(rest)
}
}