11Graph Algorithms

Embed Size (px)

Citation preview

  • 7/31/2019 11Graph Algorithms

    1/7

    Graph Algorithms

    Graph Algorithms

    Graph Theory is an area of mathematics that deals with following types of problems

    q Connection problems

    q Scheduling problems

    q Transportation problems

    q Network analysis

    q Games and Puzzles.

    he Graph Theory has important applications in Critical path analysis, Social psychology, Ma

    eory, Set theory, Topology, Group theory, Molecular chemistry, and Searching.

    hose who would like to take a quick tour of essentials of graph theory please go directly to

    Graph Theory" from here.

    igraph

    directed graph, or digraph G consists of a finite nonempty set of vertices V, and a finite set o

    dges E, where an edge is an ordered pair of vertices in V. Vertices are also commonly referre

    nodes. Edges are sometimes referred to as arcs.

    s an example, we could define a graph G=(V, E) as follows:

    V = {1, 2, 3, 4}

    E = { (1, 2), (2, 4), (4, 2) (4, 1)}

    ere is a pictorial representation of this graph.

    ttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/graphIntro.htm (1 of 7)2/24/2006 8:54:46 PM

  • 7/31/2019 11Graph Algorithms

    2/7

    Graph Algorithms

    he definition of graph implies that a graph can be drawn just knowing its vertex-set and its ed

    t. For example, our first example

    as vertex set V and edge set E where: V = {1,2,3,4} and E = {(1,2),(2,4),(4,3),(3,1),(1,4),(2,1

    ,2),(3,4),(1,3),(4,1). Notice that each edge seems to be listed twice.

    nother example, the following Petersen Graph G=(V,E) has vertex set V and edge set E wher

    = {1,2,3,4}and E ={(1,2),(2,4),(4,3),(3,1),(1,4),(2,1),(4,2),(3,4),(1,3),(4,1)}.

    ttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/graphIntro.htm (2 of 7)2/24/2006 8:54:46 PM

  • 7/31/2019 11Graph Algorithms

    3/7

    Graph Algorithms

    We'll quickly covers following three important topics from algorithmic perspective.

    1. Transpose

    2. Square

    3. Incidence Matrix

    Transpose

    graph G = (V, E) is a directed graph, its transpose, GT = (V, ET) is the same as graph G withrows reversed. We define the transpose of a adjacency matrix A = (aij) to be the adjacency

    atrix AT = (Taij) given byTaij = aji. In other words, rows of matrix A become columns of ma

    T and columns of matrix A becomes rows of matrix AT. Since in an undirected graph, (u, v)

    , u) represented the same edge, the adjacency matrix A of an undirected graph is its own

    anspose: A = AT.

    ttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/graphIntro.htm (3 of 7)2/24/2006 8:54:46 PM

  • 7/31/2019 11Graph Algorithms

    4/7

    Graph Algorithms

    ormally, the transpose of a directed graph G = (V, E) is the graph GT (V, ET), where ET = {(u

    VV : (u, v)E. Thus, GT is G with all its edges reversed.

    We can compute GT from G in the adjacency matrix representations and adjacency list

    presentations of graph G.

    lgorithm for computing GT

    from G in representation of graph G is

    ALGORITHM MATRIX TRANSPOSE (G, GT)

    For i = 0 to i < V[G]

    For j = 0 to j V[G]

    GT (j, i) = G(i, j)j = j + 1;

    i = i + 1

    o see why it works notice that if GT(i, j) is equal to G(j, i), the same thing is achieved. The tim

    mplexity is clearly O(V2).

    lgorithm for Computing GT from G in Adjacency-List Representation

    this representation, a new adjacency list must be constructed for transpose of G. Every list i

    djacency list is scanned. While scanning adjacency list ofv (say), if we encounter u, we put v

    djacency-list of u.

    ALGORITHM LIST TRANSPOSE [G]

    for u = 1 to V[G]

    for each element vAdj[u]

    Insert u into the front of Adj[v]

    ttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/graphIntro.htm (4 of 7)2/24/2006 8:54:46 PM

  • 7/31/2019 11Graph Algorithms

    5/7

    Graph Algorithms

    o see why it works, notice if an edge exists from u to v, i.e., v is in the adjacency list of u, the

    present in the adjacency list ofv in the transpose of G.

    Square

    he square of a directed graph G = (V, E) is the graph G2 = (V, E2) such that (a, b)E2 if and

    nly if for some vertex cV, both (u, c)E and (c,b)E. That is, G2 contains an edge between

    rtex a and vertex b whenever G contains a path with exactly two edges between vertex a and

    rtex b.

    Algorithms for Computing G2 from G in the Adjacency-List

    Representation of G

    Create a new array Adj'(A), indexed by V[G]

    For each v in V[G] do

    For each u in Adj[v] do

    \\ v has a path of length 2.

    \\ to each of the neighbors of u

    make a copy of Adj[u] and append it to Adj'[v]

    Return Adj'(A).

    or each vertex, we must make a copy of at most |E| list elements. The total time is O(|V| * |E|

    Algorithm for Computing G2 from G in the Adjacency-Matrix

    representation of G.

    ttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/graphIntro.htm (5 of 7)2/24/2006 8:54:46 PM

  • 7/31/2019 11Graph Algorithms

    6/7

    Graph Algorithms

    For i = 1 to V[G]

    Forj = 1 to V[G]

    For k= 1 to V[G]

    c[i,j] = c[i,j] + c[i, k] * c[k,j]

    ecause of three nested loops, the running time is O(V3).

    Incidence Matrix

    he incidence matrix of a directed graph G=(V, E) is a VE matrix B = (bij) such that

    -1 if edgej leaves vertex j.

    bij

    = 1 if edgej enters vertex j.

    0 otherwise.

    B is the incidence matrix and B

    T

    is its transpose, the diagonal of the product matrix BB

    T

    presents the degree of all the nodes, i.e., if P is the product matrix BBT then P[i,j] represent

    e degree of node i:

    pecifically we have

    BBT(i,j) = eE bie b

    Tej = eE

    bie bje

    ow,

    q Ifi =j, then biebje = 1, whenever edge e enters or leaves vertex i and 0 otherwise.

    q Ifij, then biebje = -1, when e = (i,j) or e = (j, i) and 0 otherwise.

    ttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/graphIntro.htm (6 of 7)2/24/2006 8:54:46 PM

  • 7/31/2019 11Graph Algorithms

    7/7

    Graph Algorithms

    herefore

    BBT(i,j) = deg(i) = in_deg + Out_deg if i =j

    = -(# of edges connecting i anj ) if i

    j

    http://www.personal.kent.edu/~rmuhamma/Algorithms/algorithm.html