SetColimit.hs revision c7e03d0708369f944b6f235057b39142a21599f2
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian MaederModule : $Header$
c63ebf815c8a874525cf18670ad74847f7fc7b26Christian MaederDescription : colimit of an arbitrary diagram in Set
c092fcac4b8f5c524c22ca579189c4487c13edf7Christian MaederCopyright : (c) Mihai Codescu, and Uni Bremen 2002-2006
54ed6a6b1a6c7d27fadb39ec5b59d0806c81f7c8Christian MaederLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiMaintainer : mcodescu@informatik.uni-bremen.de
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederStability : provisional
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiPortability : non-portable
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian MaederComputes the colimit of an arbitrary diagram in Set:
c092fcac4b8f5c524c22ca579189c4487c13edf7Christian Maeder - the set is the disjoint union of all sets in the diagram
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder (which we obtain by pairing elements with the node number)
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder factored by the equivalence generated by the pairs (x, f_i(x)),
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder with i an arrow in the diagram
da955132262baab309a50fdffe228c9efe68251dCui Jian - the structural morphisms are factorizations
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder computeColimitSet
0799b5dc3f06d2640e66e9ab54b8b217348fd719Christian Maeder , initialDegrees
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder , updateDegrees
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder , orderByIncomingEdges
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowskiimport qualified Data.Map as Map
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maederimport qualified Data.Set as Set
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder--the colimit is initially the empty set
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder--and the functions from each nod to this set are defined as empty
c0c2380bced8159ff0297ece14eba948bd236471Christian MaederinitColimit :: Gr (Set.Set a) (Int, EndoMap a) ->
6352f3c31da3043783a13be6594aacb2147378baRazvan Pascanu ((Set.Set (a,Node)), Map.Map Node (Map.Map a (a,Node)))
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus GrossinitColimit gr = (Set.empty, Map.fromList $ zip (nodes gr) (repeat Map.empty))
a2b04db3e156312a8596d8084f7f0f51acf8a96bChristian Maeder--each element of the sets is marked as visited or not
fc1a590cd3ee36797c0a032ff41e07f8e2469341Christian Maeder--initial marking of the elements of a set
66a774f13272fde036481edd2298081ab3d04678Razvan Pascanu--flag is set to false by default and it becomes true
834c2e71b8e390e5b05c8d02bb6eb22621125133Markus Gross--for the elements that are in the image of some set via some function
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian MaederinitialMarking :: (Ord a) => Gr (Set.Set a) b -> Map.Map Node (Map.Map a Bool)
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian MaederinitialMarking gr = let nodeList = nodes gr
6e52f1dfc0da4bc4a7701cf856641c9dce08fc7dChristian Maeder in Map.fromList $ zip nodeList $ map (markNode gr) nodeList
9f85afecbd79b3df5a0bb17bd28cd0b288dc3213Kristina SojakovamarkNode :: (Ord a) => Gr (Set.Set a) b -> Node -> Map.Map a Bool
63da71bfb4226f504944b293fb77177ebcaea7d4Ewaryst SchulzmarkNode gr n = case lab gr n of
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian Maeder Nothing -> Map.empty --shouldnt be the case
14c89b2d830777bf4db2850f038c9f60acaca486Christian Maeder Just m -> Map.fromList $ zip (Set.elems m) (repeat False)
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder--mapping assigning to each node the number of incoming edges
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder--will be updated when choosing a node
9175e29c044318498a40f323f189f9dfd50378efChristian MaederinitialDegrees :: Gr a b -> Map.Map Node Int
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian MaederinitialDegrees graph = let nodeList = nodes graph in
923e25bb8c7cf9f2978c7844ad173704482cc3b0Martin Kühl Map.fromList $ zip nodeList $ map (indeg graph) nodeList
2b873214c9ab511bbca437c036371ab664aedaceChristian MaedercomputeColimitSet :: (Ord a) => Gr (Set.Set a) (Int, EndoMap a) ->
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder ((Set.Set (a, Node)), Map.Map Node (Map.Map a (a,Node)))
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian MaedercomputeColimitSet graph =
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian Maeder if isEmpty graph then initColimit graph
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder let mark = initialMarking graph
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus Gross (cset, cfct) = initColimit graph
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus Gross incomEdges = initialDegrees graph
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder nodeList = orderedList graph incomEdges
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder in iterateNodes graph mark nodeList incomEdges cset cfct
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder--algorithm performed:
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- take the node of the graph with the least number of incoming edges
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- add its unmarked elements to the colimit and mark their images
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- update the incoming edges number for the node's succesors and
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- re-order the list according to updated function
9175e29c044318498a40f323f189f9dfd50378efChristian MaederiterateNodes :: (Ord a ) =>
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder Gr (Set.Set a) (Int, EndoMap a) ->
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder -> ((Set.Set (a,Node)), Map.Map Node (Map.Map a (a,Node)))
9175e29c044318498a40f323f189f9dfd50378efChristian MaederiterateNodes graph mark nodeList incomEdges cset cfct =
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder if nodeList == [] then (cset, cfct)
ee1c7c5796832536932d7b06cbfb1ca13f9a0d7bMartin Kühl hnode = head nodeList
ee1c7c5796832536932d7b06cbfb1ca13f9a0d7bMartin Kühl (cset1, cfct1, mark1) = case lab graph hnode of
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder Nothing -> (cset, cfct, mark)
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder Just m -> addUnmarked (Set.elems m) hnode graph mark cset cfct
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder incomEdges1 = updateDegrees graph hnode incomEdges
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder newList = orderByIncomingEdges (tail nodeList) graph incomEdges1
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder in iterateNodes graph mark1 newList incomEdges1 cset1 cfct1
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder--decrease the number of incoming edges for all the successors of a node
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian MaederupdateDegrees :: Gr a b -> Node -> Map.Map Node Int -> Map.Map Node Int
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian MaederupdateDegrees graph hnode incomEdges = let
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder nList = suc graph hnode
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder insertFromList f list = case list of
7bbfb15142ab4286dfc6fcde2fc94a5512297e41Jonathan von Schroeder x:xs -> let oldValue = (Map.!) f x
fa388aea9cef5f9734fec346159899a74432ce26Christian Maeder f1 = Map.insert x (oldValue-1) f
63719301448519453f66383f4e583d9fd5b89ecbChristian Maeder in insertFromList f1 xs
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder in insertFromList incomEdges nList
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder-- adds the equivalence classes of the elements of a set to the colimit
52d922076b89f12234f721974e82531bc69a6f69Christian MaederaddUnmarked :: (Ord a) => [a] -> Node -> Gr (Set.Set a)(Int, EndoMap a)
923e25bb8c7cf9f2978c7844ad173704482cc3b0Martin Kühl -> Map.Map Node (Map.Map a Bool) -> (Set.Set (a,Node))
72079df98b3cb7cc1fd82a0a24984893dcd05ecaEwaryst Schulz -> ((Set.Set (a,Node)),
fbc4f8708092d571a45cb483f37cc6b674da45a7Christian MaederaddUnmarked elemlist node graph mark cset cfct = case elemlist of
8a77240a809197c92c0736c431b4b88947a7bac1Christian Maeder [] -> (cset, cfct, mark)
8a77240a809197c92c0736c431b4b88947a7bac1Christian Maeder a:xs -> if (Map.!) ((Map.!) mark node) a then
fbc4f8708092d571a45cb483f37cc6b674da45a7Christian Maeder addUnmarked xs node graph mark cset cfct
bb63f684c4f5f33ffcd1dcc02c58d6a703900fafJonathan von Schroeder -- do not remove test, because the graph may have cycles
b0234f0a84fcd3587073fbc11d38759108997c3cChristian Maeder (cset1, cfct1, mark1) = addClass (a,node) graph mark (cset,cfct)
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus Gross in addUnmarked xs node graph mark1 cset1 cfct1
d56ece59c372cb887355825901222b9f3377f7e6Thiemo WiedemeyerorderedList :: (Ord a) => Gr a b -> Map.Map Node Int -> [Node]
d56ece59c372cb887355825901222b9f3377f7e6Thiemo WiedemeyerorderedList gr incomEdges = orderByIncomingEdges (nodes gr) gr incomEdges
9175e29c044318498a40f323f189f9dfd50378efChristian MaederorderByIncomingEdges :: [Node] -> Gr a b -> Map.Map Node Int -> [Node]
f1dec6898638ba1131a9fadbc4d1544c93dfabb0Klaus LuettichorderByIncomingEdges [] _ _ = []
9175e29c044318498a40f323f189f9dfd50378efChristian MaederorderByIncomingEdges (x:xs) gr incomEdges =
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maeder ( orderByIncomingEdges
let fx = Map.insert x1 y1 ((Map.!) f x)
in Map.insert x fx f
addClass :: (Ord a) => (a,Node) -> Gr (Set.Set a) (Int, EndoMap a)
((Set.Set (a,Node)),
cset1 = Set.insert (a, node) cset;--add a new element to the colimit
applyWithDefault f x = if x `elem` Map.keys f then (Map.!) f x else x
Just m -> filter (\x -> applyWithDefault label x == y) $ Set.elems m