-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDfs.hs
More file actions
55 lines (47 loc) · 1.85 KB
/
Dfs.hs
File metadata and controls
55 lines (47 loc) · 1.85 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
module Graph.Dfs where
import Data.List
import Prelude hiding (odd, even)
type Node = Int
type Edge = (Node,Node)
type Graph = ([Node],[Edge])
depthfirst :: Graph -> Node -> [Node]
depthfirst (v,e) n
| [x|x<-v,x==n] == [] = []
| otherwise = dfrecursive (v,e) [n]
dfrecursive :: Graph -> [Node] -> [Node]
dfrecursive ([],_) _ = []
dfrecursive (_,_) [] = []
dfrecursive (v,e) (top:stack)
| [x|x<-v,x==top] == [] = dfrecursive (newv, e) stack
| otherwise = top : dfrecursive (newv, e) (adjacent ++ stack)
where
adjacent = [x | (x,y)<-e,y==top] ++ [x | (y,x)<-e,y==top]
newv = [x|x<-v,x/=top]
connectedcomponents :: Graph -> [[Node]]
connectedcomponents ([],_) = []
connectedcomponents (top:v,e)
| remaining == [] = [connected]
| otherwise = connected : connectedcomponents (remaining, e)
where
connected = depthfirst (top:v,e) top
remaining = (top:v) \\ connected
dfsbipartite :: Graph -> [(Node, Int)] -> [Node] -> [Node] -> Bool
dfsbipartite ([],_) _ _ _ = True
dfsbipartite (_,_) [] _ _ = True
dfsbipartite (v,e) ((nv, 0):stack) odd even
| [x|x<-v,x==nv] == [] = dfsbipartite (v, e) stack odd even
| [] == intersect adjacent even = dfsbipartite (newv, e) ([(x,1)|x<-adjacent] ++ stack) odd (nv : even)
| otherwise = False
where
adjacent = [x | (x,y)<-e,y==nv] ++ [x | (y,x)<-e,y==nv]
newv = [x|x<-v,x/=nv]
dfsbipartite (v,e) ((nv, 1):stack) odd even
| [x|x<-v,x==nv] == [] = dfsbipartite (v, e) stack odd even
| [] == intersect adjacent odd = dfsbipartite (newv, e) ([(x,0)|x<-adjacent] ++ stack) (nv : odd) even
| otherwise = False
where
adjacent = [x | (x,y)<-e,y==nv] ++ [x | (y,x)<-e,y==nv]
newv = [x|x<-v,x/=nv]
bipartite :: Graph -> Bool
bipartite ([],_) = True
bipartite (top:v,e) = dfsbipartite (top:v, e) [(top,0)] [] []