-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph.hs
217 lines (183 loc) · 7.05 KB
/
Graph.hs
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
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
-- | A simple and slow directed graph implementation using only lists and pairs.
module Graph
( Graph (..)
, vertex
, element
, node
, nodes
, edges
, vertices
, verticesToNodes
, nodesToVertices
, empty
, isEmpty
, existsNode
, existsEdge
, existsSelfLoop
, addNode
, addEdge
, fromList
, fromNodeList
, fromNodeList1
, fromEdgeList
, deleteNode
, deleteEdge
, adjacents
, putNodeElement
, getNodeElement
, depthFirstSearch
, breadthFirstSearch
, acyclic
, existsPath
) where
import Data.List as List
type Vertex = Int
type Element = String
type Edge = (Vertex, Vertex)
type Node = (Vertex, Element)
type Graph = ([Node], [Edge])
instance {-# OVERLAPS #-} Show Graph where
show g = "\n Graph:" ++ "\n"
++ "\tVertices: " ++ "{" ++ List.intercalate "," [show v | v <- vertices g] ++ "}" ++ "\n"
++ "\tEdges: " ++ "{" ++ List.intercalate "," [show e | e <- edges g] ++ "}" ++ "\n"
++ "\tElements: " ++ "{" ++ List.intercalate "," [show (element n) | n <- nodes g] ++ "}\n"
instance {-# OVERLAPS #-} Show Node where
show n = show (vertex n)
-- Get the vertex of a given node
vertex :: Node -> Vertex
vertex n = fst n
-- Get the element of a given node
element :: Node -> Element
element n = snd n
-- All nodes of the graph
nodes :: Graph -> [Node]
nodes g = fst g
-- All edges of the graph
edges :: Graph -> [Edge]
edges g = snd g
-- All vertices of the graph
vertices :: Graph -> [Vertex]
vertices g = map vertex (nodes g)
-- Get a node given its vertex id
node :: Vertex -> Graph -> Node
node v g
| v == vertex (head (nodes g)) = head (nodes g)
| otherwise = node v (tail (nodes g), edges g)
-- List of nodes from a list of vertices
verticesToNodes :: [Vertex] -> Graph -> [Node]
verticesToNodes [] _ = []
verticesToNodes (v : vs) g = node v g : verticesToNodes vs g
-- List of vertices from a list of nodes
nodesToVertices :: [Node] -> [Vertex]
nodesToVertices [] = []
nodesToVertices (v : vs) = vertex v : nodesToVertices vs
-- Create an empty graph
empty :: Graph
empty = ([], [])
-- Check if the graph has at least one node
isEmpty :: Graph -> Bool
isEmpty ([], _) = True
isEmpty g = False
-- Check if a node exists
existsNode :: Vertex -> Graph -> Bool
existsNode _ ([], _) = False
existsNode v g = any (\x -> x == v) (vertices g)
-- Check if an edge between two nodes exists
existsEdge :: Edge -> Graph -> Bool
existsEdge (u, v) g = any (\(x, y) -> x == u && y == v) (edges g)
-- Check if there is at least one self loop inside the graph
existsSelfLoop :: Edge -> Graph -> Bool
existsSelfLoop (u, v) g = any (\(x, y) -> x == y) (edges g)
-- Add a node to the graph if its vertex doesn't exist
addNode :: Graph -> Node -> Graph
addNode g n
| existsNode (vertex n) g = g
| otherwise = (n:(nodes g), edges g)
-- Add a new edge to the graph only if both nodes exist and if the edge already doesn't exist
addEdge :: Graph -> Edge -> Graph
addEdge g (u, v)
| (existsNode u g) && (existsNode v g) && (not(existsEdge (u, v) g)) = (nodes g, (u, v):(edges g))
| otherwise = g
-- Create a new graph from a list of vertices
fromList :: [Vertex] -> Graph
fromList [] = empty
fromList v = addNode (fromList (tail v)) (head v, "")
-- Create a new graph from a list of nodes
fromNodeList :: [Node] -> Graph
fromNodeList = foldl addNode empty
-- Add a new list of nodes to an existing graph
fromNodeList1 :: Graph -> [Node] -> Graph
fromNodeList1 g = foldl addNode g
-- Add a new list of edges to an existing graph
fromEdgeList :: Graph -> [Edge] -> Graph
fromEdgeList g = foldl addEdge g
-- Delete a node only if it is not connected to any node
deleteNode :: Vertex -> Graph -> Graph
deleteNode v g
| (existsNode v g) && not (any (\(x, y) -> x == v || y == v) (edges g)) = (deleteN v (nodes g), edges g)
| otherwise = g
where deleteN u xs = if (vertex (head xs)) == u then tail xs else head xs : deleteN u (tail xs)
-- Delete an edge if exists
deleteEdge :: Edge -> Graph -> Graph
deleteEdge (u, v) g
| existsNode u g && existsNode v g && existsEdge(u, v) g = (nodes g, deleteA u v (edges g))
| otherwise = g
where deleteA z k xs = if ((fst (head xs) == z) && (snd (head xs) == k)) then tail xs else head xs : deleteA z k (tail xs)
-- List of adjacent nodes of a given vertex
adjacents :: Vertex -> Graph -> [Node]
adjacents v g
| existsNode v g = verticesToNodes (adjacentNodes v (edges g)) g
| otherwise = []
where adjacentNodes v [] = []
adjacentNodes v (x : xs)
| fst x == v = snd x : adjacentNodes v xs
| otherwise = adjacentNodes v xs
-- Update the element contained in a node
putNodeElement :: Vertex -> Element -> Graph -> Graph
putNodeElement v e g
| existsNode v g = (putContent v e (nodes g), edges g)
| otherwise = g
where putContent u k nodes = if (vertex (head nodes)) == u then (u, k) : tail nodes else head nodes : putContent u k (tail nodes)
-- Read the element contained in a node
getNodeElement :: Vertex -> Graph -> Maybe Element
getNodeElement v g
| existsNode v g = Just (element (node v g))
| otherwise = Nothing
-- Depth First Search
depthFirstSearch :: Vertex -> Graph -> [Node]
depthFirstSearch v g
| existsNode v g = if (null (adjacents v g)) then [node v g] else dfs [v] [node v g] g
| otherwise = []
where dfs _ [] _ = []
dfs vs (n:ns) g = n : dfs (vs ++ [vertex n]) (inStackNodes (vertex n) (vs ++ [vertex n]) (adjacents (vertex n) g) ns) g
where inStackNodes v ns [] xs = xs
inStackNodes v ns vs xs = (filter (\x-> (not(any (\y -> x == y || v == (vertex x)) xs))) (filter (\z -> not(elem (vertex z) ns)) vs)) ++ xs
-- Breadth First Search
breadthFirstSearch :: Vertex -> Graph -> [Node]
breadthFirstSearch v g
| existsNode v g = if (null(adjacents v g)) then [node v g] else bfs [v] [node v g] g
| otherwise = []
where bfs _ [] _ = []
bfs vs (n:ns) g = n : bfs (vs ++ [vertex n]) (inQueueNodes (vertex n) (vs ++ [vertex n]) (adjacents (vertex n) g) ns) g
where inQueueNodes v ns [] xs = xs
inQueueNodes v ns vs xs = xs ++ (filter (\x -> (not (any (\y->x == y || v == (vertex x)) xs))) (filter (\z-> not (elem (vertex z) ns)) vs))
-- Verify the absence of cycles inside the graph
acyclic :: Graph -> Bool
acyclic g
| isEmpty g = True
| otherwise = not (any (\x-> existsPath x x g) (vertices g))
-- Verify that a path between two nodes exists. Self-loop are not considered
existsPath :: Vertex -> Vertex -> Graph -> Bool
existsPath _ _ (_, []) = False
existsPath u v g
| (existsNode u g) && (existsNode v g) = path v [u] [(node u g)] g
| otherwise = False
where path _ _ [] _ = False
path u vs (n : ns) g
| (not (existsEdge ((vertex n), u) g)) && (not (any (\x -> vertex x == u)(ns))) = go
| otherwise = True
where go = path u (vs ++ [vertex n]) (inQueueNodes (vertex n) (vs ++ [vertex n]) (adjacents (vertex n) g) ns) g
inQueueNodes v ns [] xs = xs
inQueueNodes v ns vs xs = xs ++ (filter (\x -> (not (any (\y->x == y || v == (vertex x)) xs))) (filter (\z-> not (elem (vertex z) ns)) vs))