|
| 1 | +import networkx as nx |
1 | 2 |
|
2 |
| - "properties": { |
| 3 | +# Metadata-like structure for properties |
| 4 | +properties = { |
3 | 5 | "id": {
|
4 |
| - "description": "Lowercase, underscore-separated, unique identifier.", |
5 |
| - "pattern": "^[a-z0-9_]+$", |
6 |
| - "title": "Id", |
7 |
| - "type": "string" |
| 6 | + "description": "Lowercase, underscore-separated, unique identifier.", |
| 7 | + "pattern": "^[a-z0-9_]+$", |
| 8 | + "title": "Id", |
| 9 | + "type": "string" |
8 | 10 | },
|
9 | 11 | "name": {
|
10 |
| - "description": "Human-readable format, title case with acronyms.", |
11 |
| - "title": "Name", |
12 |
| - "type": "string" |
| 12 | + "description": "Human-readable format, title case with acronyms.", |
| 13 | + "title": "Name", |
| 14 | + "type": "string" |
13 | 15 | },
|
14 | 16 | "full_form": {
|
15 |
| - "description": "Full acronym expansion.", |
16 |
| - "title": "Full Form", |
17 |
| - "type": "string" |
18 |
| - }, |
19 |
| - "category": { |
20 |
| - "description": "Lowercase, underscore-separated.", |
21 |
| - "pattern": "^[a-z0-9_]+$", |
22 |
| - "title": "Category", |
23 |
| - "type": "string" |
24 |
| - }, |
25 |
| - "layer": { |
26 |
| - "description": "Lowercase, underscore-separated.", |
27 |
| - "pattern": "^[a-z0-9_]+$", |
28 |
| - "title": "Layer", |
29 |
| - "type": "string" |
30 |
| - }, |
31 |
| - "system": { |
32 |
| - "description": "System environment or domain.", |
33 |
| - "title": "System", |
34 |
| - "type": "string" |
35 |
| - }, |
36 |
| - "utility": { |
37 |
| - "description": "Lowercase values; multiple roles supported.", |
38 |
| - "items": { |
| 17 | + "description": "Full acronym expansion.", |
| 18 | + "title": "Full Form", |
39 | 19 | "type": "string"
|
40 |
| - }, |
41 |
| - "title": "Utility", |
42 |
| - "type": "array" |
43 |
| - }, |
44 |
| - "description": { |
45 |
| - "anyOf": [ |
46 |
| - { |
47 |
| - "type": "string" |
48 |
| - }, |
49 |
| - { |
50 |
| - "type": "null" |
51 |
| - } |
52 |
| - ], |
53 |
| - "default": null, |
54 |
| - "description": "Optional description of the node.", |
55 |
| - "title": "Description" |
56 |
| - }, |
57 |
| - "connections": { |
58 |
| - "anyOf": [ |
59 |
| - { |
60 |
| - "items": { |
61 |
| - "type": "string" |
62 |
| - }, |
63 |
| - "type": "array" |
64 |
| - }, |
65 |
| - { |
66 |
| - "type": "null" |
67 |
| - } |
68 |
| - ], |
69 |
| - "default": null, |
70 |
| - "description": "Optional list of connected node IDs.", |
71 |
| - "title": "Connections" |
72 |
| - }, |
73 |
| - "domain": { |
74 |
| - "anyOf": [ |
75 |
| - { |
76 |
| - "type": "string" |
77 |
| - }, |
78 |
| - { |
79 |
| - "type": "null" |
80 |
| - } |
81 |
| - ], |
82 |
| - "default": null, |
83 |
| - "description": "Optional domain the node belongs to.", |
84 |
| - "title": "Domain" |
85 |
| - }, |
86 |
| - "thresholds": { |
87 |
| - "anyOf": [ |
88 |
| - { |
89 |
| - "additionalProperties": { |
90 |
| - "type": "number" |
91 |
| - }, |
92 |
| - "type": "object" |
93 |
| - }, |
94 |
| - { |
95 |
| - "type": "null" |
96 |
| - } |
97 |
| - ], |
98 |
| - "default": null, |
99 |
| - "description": "Optional thresholds for triggering actions.", |
100 |
| - "title": "Thresholds" |
101 |
| - }, |
102 |
| - "status": { |
103 |
| - "anyOf": [ |
104 |
| - { |
105 |
| - "type": "string" |
106 |
| - }, |
107 |
| - { |
108 |
| - "type": "null" |
109 |
| - } |
110 |
| - ], |
111 |
| - "default": null, |
112 |
| - "description": "Optional status indicator, e.g., active, deprecated.", |
113 |
| - "title": "Status" |
114 |
| - }, |
115 |
| - "tags": { |
116 |
| - "anyOf": [ |
117 |
| - { |
118 |
| - "items": { |
119 |
| - "type": "string" |
120 |
| - }, |
121 |
| - "type": "array" |
122 |
| - }, |
123 |
| - { |
124 |
| - "type": "null" |
125 |
| - } |
126 |
| - ], |
127 |
| - "default": null, |
128 |
| - "description": "Optional list of tags or keywords.", |
129 |
| - "title": "Tags" |
130 | 20 | }
|
131 |
| - }, |
132 |
| - "required": [ |
133 |
| - "id", |
134 |
| - "name", |
135 |
| - "full_form", |
136 |
| - "category", |
137 |
| - "layer", |
138 |
| - "system", |
139 |
| - "utility" |
140 |
| - ], |
141 |
| - "title": "Node", |
142 |
| - "type": "object" |
143 | 21 | }
|
| 22 | + |
| 23 | +# Depth-First Search (DFS) function with metadata |
| 24 | +def dfs(graph, start, max_depth=None): |
| 25 | + """ |
| 26 | + Perform Depth-First Search (DFS) from a starting node. |
| 27 | + |
| 28 | + Parameters: |
| 29 | + - graph: The graph object (directed or undirected). |
| 30 | + - start: The node to start the traversal. |
| 31 | + - max_depth: The maximum depth to search (optional, None for no limit). |
| 32 | + |
| 33 | + Returns: |
| 34 | + - A list of nodes visited during the traversal. |
| 35 | + """ |
| 36 | + visited, result = set(), [] |
| 37 | + |
| 38 | + def _dfs(node, depth): |
| 39 | + if node not in visited and (max_depth is None or depth <= max_depth): |
| 40 | + visited.add(node) |
| 41 | + result.append(node) |
| 42 | + for neighbor in graph.neighbors(node): |
| 43 | + _dfs(neighbor, depth + 1) |
| 44 | + |
| 45 | + _dfs(start, 0) |
| 46 | + return result |
| 47 | + |
| 48 | +# Breadth-First Search (BFS) function with metadata |
| 49 | +def bfs(graph, start, max_depth=None): |
| 50 | + """ |
| 51 | + Perform Breadth-First Search (BFS) from a starting node. |
| 52 | + |
| 53 | + Parameters: |
| 54 | + - graph: The graph object (directed or undirected). |
| 55 | + - start: The node to start the traversal. |
| 56 | + - max_depth: The maximum depth to search (optional, None for no limit). |
| 57 | + |
| 58 | + Returns: |
| 59 | + - A list of nodes visited during the traversal. |
| 60 | + """ |
| 61 | + visited, queue, result, depth = set(), [start], [], 0 |
| 62 | + |
| 63 | + while queue and (max_depth is None or depth <= max_depth): |
| 64 | + node = queue.pop(0) |
| 65 | + if node not in visited: |
| 66 | + visited.add(node) |
| 67 | + result.append(node) |
| 68 | + queue.extend(graph.neighbors(node)) |
| 69 | + if not queue: |
| 70 | + depth += 1 |
| 71 | + return result |
| 72 | + |
| 73 | +# Example: Graph Creation and Usage of DFS/BFS |
| 74 | +if __name__ == "__main__": |
| 75 | + G = nx.Graph() |
| 76 | + G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 5)]) |
| 77 | + |
| 78 | + # Example DFS and BFS |
| 79 | + print("DFS Traversal:", dfs(G, 1)) |
| 80 | + print("BFS Traversal:", bfs(G, 1)) |
| 81 | + |
| 82 | + # Output metadata properties |
| 83 | + print("\nMetadata Properties:") |
| 84 | + for key, value in properties.items(): |
| 85 | + print(f"{key.capitalize()}:") |
| 86 | + for k, v in value.items(): |
| 87 | + print(f" {k}: {v}") |
0 commit comments