Graphs in Computer Science


Introduction

Graphs are mathematical concepts that have found many uses in computer science. Graphs come in many different flavors, many of which have found uses in computer programs. Some flavors are:

Most graphs are defined as a slight alteration of the following rules.

Adjacency. If (u,v) is in the edge set we say u is adjacent to v (which we sometimes write as u ~ v).

For example the graph drawn below:

?

Has the following parts.


Kinds of Graphs

Various flavors of graphs have the following specializations and particulars about how they are usually drawn.


Representing graphs in a computer

Graphs are often used to represent physical entities (a network of roads, the relationship between people, etc) inside a computer. There are numerous mechansims used. A good choice of mechanism depends upon the operations that the computer program needs to perform on the graph to acheive its needs. Possible operations include.

Not all programs will need all of these operations, so for some programs, an efficent representation that can compute only the operations needed (but not the others), will suffice.

Advantages of representing graphs as functions

Disadvantages of using graphs as functions

  • Graphs as arrays of adjacent vertexes.

    One mechanism that can ameliorate the disadvantages of using functions as a way to represent graphs is to use arrays instead. Using this mechanism requires that the underlying domain of Vertices be some type that can be used as indexes into an array.

    In the rest of this note we will assume that Vertices are of type Int, and that the Vertices set is a finite range of the type Int. Thus a graph can be represented as follows:

    type ArrGraph = Array [Int]
    
    We can now answer a number queries about graphs quickly and efficiently.
    type ArrGraph i = Array [i]
    
    vertices:: ArrGraph i -> IO[Int]
    edges:: ArrGraph i -> IO[(Int,i)]
    children:: ArrGraph i -> i -> IO[i]
    
    vertices g =
      do { (lo,hi) <- boundsArr g
         ; return [lo..hi]}
    
    edges g =
      do { (lo,hi) <- boundsArr g
         ; ees <- toListArr g
         ; return [ (i,j) | (i,cs) <- zip [lo..hi] ees, j <- cs ] }
    
    children g node = readArr g node
    

    Advantages of representing graphs as arrays

    Disadvantages of representing graphs as arrays


    Algorithms on Graphs

    There are many, many algorithms on graphs. In this note we will look at a few of them. They include:

    See the code for some examples.

    Back to the Daily Record.

    Back to the class web-page.