Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

How to find the laplacian matrix of a weighted directed graph

asked 7 years ago

anonymous user

Anonymous

How to find the laplacian matrix of a weighted directed graph? Please explain with an example

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 7 years ago

B r u n o gravatar image
sage: G = DiGraph(3, weighted=True)
sage: G.add_edges([(0,2,4), (1,2,6),(0,1,2)])
sage: G.laplacian_matrix()

[ 0 -2 -4]
[ 0  2 -6]
[ 0  0 10]
Preview: (hide)
link
0

answered 7 years ago

dan_fulea gravatar image

This is an alternative longer answer...

One problem may be, depending on the situation, to initialize and/or declare the "weight data" for the di(rected )graph. The following assumes that the weights are given in a matrix A, then it constructs in one line the corresponding digraph. The method laplacian_matrix, applied on it gives the result. Sample code follows. Instead of typing myself the entries of a matrix, there will be a random generation.

import random
random.seed( 20171101 )

def myrandomizer():
    if random.uniform( 0,1 ) < 0.7:
        return 0
    return random.choice( [ 1..10 ] )

N = 8
A = matrix( QQ, N, N, [ myrandomizer() for _ in range(N^2) ] )
print A

D = DiGraph( A, format='weighted_adjacency_matrix' )
# D.show()    # decomment to show...

The only important line above, given A is the one with D = DiGraph( A, format='weighted_adjacency_matrix' ) . (Without specifying the format, sage is trying to guess the format, and it rather generates multiple edges between two vertices for a (weight) entry 2.)

The print A line gives the reproducible random matrix

[ 7  0  0  0  0  3  2  0]
[ 0  0  0  0  7 10  0  0]
[ 0  0  0  0  0  6  3  0]
[10  0  6  0  0  0  0  0]
[ 8  7  9  3  0  3  0  9]
[ 0  0  5  0  0  0  0  0]
[ 0  5  0  0  0  0  0  2]
[ 0  0  0  0  3  6  0  0]
sage:

In the given situation we can apply the following related methods:

sage: D.laplacian_matrix()
[ 18   0   0   0   0  -3  -2   0]
[  0  12   0   0  -7 -10   0   0]
[  0   0  20   0   0  -6  -3   0]
[-10   0  -6   3   0   0   0   0]
[ -8  -7  -9  -3  10  -3   0  -9]
[  0   0  -5   0   0  28   0   0]
[  0  -5   0   0   0   0   5  -2]
[  0   0   0   0  -3  -6   0  11]

sage: D.adjacency_matrix()
[1 0 0 0 0 1 1 0]
[0 0 0 0 1 1 0 0]
[0 0 0 0 0 1 1 0]
[1 0 1 0 0 0 0 0]
[1 1 1 1 0 1 0 1]
[0 0 1 0 0 0 0 0]
[0 1 0 0 0 0 0 1]
[0 0 0 0 1 1 0 0]

sage: D.weighted()
True

sage: D.weighted_adjacency_matrix()
[ 7  0  0  0  0  3  2  0]
[ 0  0  0  0  7 10  0  0]
[ 0  0  0  0  0  6  3  0]
[10  0  6  0  0  0  0  0]
[ 8  7  9  3  0  3  0  9]
[ 0  0  5  0  0  0  0  0]
[ 0  5  0  0  0  0  0  2]
[ 0  0  0  0  3  6  0  0]
sage:

Note that the laplacian matrix differs from minus the weighted adjacency matrix only on the diagonal. The diagonal entries are adjusted, so that all sums of the columns vanish. Some books define the laplacian so that the sums on the rows vanish.

Preview: (hide)
link

Comments

Thanks for you answer. I have got the idea now.thanks

rewi gravatar imagerewi ( 7 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 7 years ago

Seen: 1,386 times

Last updated: Nov 01 '17