{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ ":1:1: error:\n Could not find module ‘Data.Tuple.Extra’\n Use -v to see a list of the files searched for." ] } ], "source": [ ":set -XNoImplicitPrelude\n", ":set -XTupleSections\n", "import Papa\n", "import qualified Numeric.LinearAlgebra as HM\n", "import qualified Numeric.LinearAlgebra.Data as HM\n", "-- import Algebra.Graph.AdjacencyMap\n", "import qualified Algebra.Graph.Labelled.AdjacencyMap as GL\n", "import qualified Algebra.Graph.AdjacencyMap as G\n", "import Algebra.Graph.Label\n", "import qualified Data.Map as M (fromList, Map(..))\n", "import Data.Tuple (swap)\n", "import Data.Tuple.Extra ((***))\n", "import Foreign.Storable (Storable(..))\n", "import Prelude (undefined, enumFromTo)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "prob :: Num t => [[t]]\n", "prob = [[-1, 1, -1, 1, -1],\n", " [-1, -1, -1, 2, -1],\n", " [2, -1, -1, -1, -1],\n", " [-1, 3, -1, -1, 2],\n", " [-1, 1, -1, -1, -1]]\n", " \n", "m :: HM.Matrix Double\n", "m = HM.fromLists prob\n", "\n", "data Vert a = Row a | Col a deriving (Ord, Show, Eq)\n", "\n", "withIndex :: [a] -> [(Int, a)]\n", "withIndex xs = zip [0 .. length xs] xs\n", "\n", "onlyVals :: (Num a, Ord a) => [(Int, a)] -> [(Int, a)]\n", "onlyVals = Papa.filter ( (> 0) . snd)\n", "\n", "row :: (a -> b) -> (Int, a) -> (Vert Int, b)\n", "row f = Row *** f\n", "\n", "col :: (a -> b) -> (Int, a) -> (Vert Int, b)\n", "col f = Col *** f\n", "\n", "rowColGraph :: [HM.Vector Double] -> GL.AdjacencyMap (Distance (Sum Double)) (Vert Int)\n", "rowColGraph = GL.fromAdjacencyMaps . \n", " ((row (M.fromList . (col (distance . return . return) <$>) . onlyVals . withIndex . HM.toList)) <$>) .\n", " withIndex\n", "\n", "rowGraph :: [HM.Vector Double] -> GL.AdjacencyMap (Distance (Sum Double)) (Vert Int)\n", "rowGraph xs = GL.edges $ \n", " ((distance 0,,) <$> fst <*> snd) <$>\n", " G.edgeList (G.path $ (Row <$> [0 .. length xs - 1]))\n", "\n", "adjGraph :: HM.Matrix Double -> GL.AdjacencyMap (Distance (Sum Double)) (Vert Int)\n", "adjGraph mat = let rs = HM.toRows mat \n", " a = rowColGraph rs\n", " b = rowGraph rs\n", " in\n", " GL.overlay b a" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "digraph \n", "{\n", " \"Row 0\"\n", " \"Row 1\"\n", " \"Row 2\"\n", " \"Row 3\"\n", " \"Row 4\"\n", " \"Col 0\"\n", " \"Col 1\"\n", " \"Col 3\"\n", " \"Col 4\"\n", " \"Row 0\" -> \"Row 1\"\n", " \"Row 0\" -> \"Col 1\"\n", " \"Row 0\" -> \"Col 3\"\n", " \"Row 1\" -> \"Row 2\"\n", " \"Row 1\" -> \"Col 3\"\n", " \"Row 2\" -> \"Row 3\"\n", " \"Row 2\" -> \"Col 0\"\n", " \"Row 3\" -> \"Row 4\"\n", " \"Row 3\" -> \"Col 1\"\n", " \"Row 3\" -> \"Col 4\"\n", " \"Row 4\" -> \"Col 1\"\n", "}" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "digraph \n", "{\n", " \"Row 0\"\n", " \"Row 1\"\n", " \"Row 2\"\n", " \"Row 3\"\n", " \"Row 4\"\n", " \"Col 0\"\n", " \"Col 1\"\n", " \"Col 3\"\n", " \"Col 4\"\n", " \"Row 0\" -> \"Row 1\"\n", " \"Row 0\" -> \"Row 2\"\n", " \"Row 0\" -> \"Row 3\"\n", " \"Row 0\" -> \"Row 4\"\n", " \"Row 0\" -> \"Col 0\"\n", " \"Row 0\" -> \"Col 1\"\n", " \"Row 0\" -> \"Col 3\"\n", " \"Row 0\" -> \"Col 4\"\n", " \"Row 1\" -> \"Row 2\"\n", " \"Row 1\" -> \"Row 3\"\n", " \"Row 1\" -> \"Row 4\"\n", " \"Row 1\" -> \"Col 0\"\n", " \"Row 1\" -> \"Col 1\"\n", " \"Row 1\" -> \"Col 3\"\n", " \"Row 1\" -> \"Col 4\"\n", " \"Row 2\" -> \"Row 3\"\n", " \"Row 2\" -> \"Row 4\"\n", " \"Row 2\" -> \"Col 0\"\n", " \"Row 2\" -> \"Col 1\"\n", " \"Row 2\" -> \"Col 4\"\n", " \"Row 3\" -> \"Row 4\"\n", " \"Row 3\" -> \"Col 1\"\n", " \"Row 3\" -> \"Col 4\"\n", " \"Row 4\" -> \"Col 1\"\n", "}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import Algebra.Graph.AdjacencyMap.Algorithm\n", "\n", "import Algebra.Graph.ToGraph\n", "import Algebra.Graph.Export.Dot\n", "\n", "a = adjGraph m\n", "tra = GL.transitiveClosure a\n", "putStrLn $ exportViaShow a\n", "putStrLn $ exportViaShow tra\n", "-- GL.adjacencyMap a\n", "-- putStrLn $ exportViaShow $ GL.transitiveClosure a\n", "-- GL.adjacencyMap tra\n" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "edges [(vertex 1,vertex 2),(vertex 1,vertex 3),(vertex 3,vertex 4),(vertex 3,vertex 5)]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import Algebra.Graph\n", "import Data.Tree\n", "\n", "scc $ toAdjacencyMap $ tree (Node 1 [Node 2 [], Node 3 [Node 4 [], Node 5 []]]) " ] } ], "metadata": { "kernelspec": { "display_name": "Haskell", "language": "haskell", "name": "haskell" }, "language_info": { "codemirror_mode": "ihaskell", "file_extension": ".hs", "name": "haskell", "pygments_lexer": "Haskell", "version": "8.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }