Skip to content

Instantly share code, notes, and snippets.

@thsutton
Last active October 27, 2015 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thsutton/3f67d5b65c06f3ace318 to your computer and use it in GitHub Desktop.
Save thsutton/3f67d5b65c06f3ace318 to your computer and use it in GitHub Desktop.
Count n-length paths through a graph by matrix multiplication

Counting paths through a graph

You can count the number of n-long paths in a graph by taking the adjacency matrix for the graph and exponentiating it by n.

digraph {
1 -> 1;
1 -> 2;
1 -> 3;
1 -> 4;
2 -> 3;
3 -> 4;
4 -> 4;
}
Copyright Thomas Sutton (c) 2015
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Thomas Sutton nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{-# LANGUAGE TupleSections #-}
module Main where
import System.Environment
import Data.Graph
import Data.GraphViz
import Data.Text (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.IO as T
import Sparse.Matrix
main :: IO ()
main = do
[ k , f ] <- getArgs
let n = read k :: Int
g <- (parseDotGraphLiberally . T.fromStrict) <$> T.readFile f
paths n g
paths :: Int -> DotGraph Int -> IO ()
paths k g = do
let m = adjacency g
print m
print (m ^ k)
adjacency :: DotGraph Int -> Mat Word
adjacency g =
let es = graphEdges g
diag = adj $ map (\e -> Key (from e) (to e)) es
bot = adj $ map (\e -> Key (to e) (from e)) $ filter (\e -> from e /= to e) es
in diag + bot
where
from = fromIntegral . fromNode
to = fromIntegral . toNode
adj = fromList . map (,1)
name: paths
version: 0.1.0.0
synopsis: Count paths in a graph.
description: Please see README.md
homepage: https://gist.github.com/thsutton/3f67d5b65c06f3ace318
license: BSD3
license-file: LICENSE
author: Thomas Sutton
maintainer: me@thomas-sutton.id.au
copyright: 2015 Thomas Sutton
category: Graphs
build-type: Simple
cabal-version: >=1.10
executable paths
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, containers
, graphviz
, sparse
, text
import Distribution.Simple
main = defaultMain
resolver: lts-3.4
packages:
- '.'
extra-deps:
- sparse-0.9.2
- hybrid-vectors-0.2
- graphviz-2999.18.0.1
flags: {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment