Вы находитесь на странице: 1из 7

Biograph object

Data structure containing generic interconnected data used to implement directed graph

Description

A biograph object is a data structure containing generic interconnected data used to


Implement a directed graph. Nodes represent proteins, genes, or any other biological entity,
and edges represent interactions, dependences, or any other relationship between the nodes.
A biograph object also stores information, such as color properties and text label characteristics,
used to create a 2-D visualization of the graph.
We can create a biograph object using the object constructor function biograph and view
a graphical representation of a biograph object using the view method.

Property Summary
A biograph object contains two kinds of objects, node objects and edge objects, that have their
own properties.

How to Create a Biograph object in MatLab

Using built-in , Create a biograph object with custom node IDs.


View the biograph object

Matlab Code

clear all;
close all:
% Create a biograph object with custom node IDs.
w = [0 1 1 0 0;1 0 0 1 1;1 0 0 0 0;0 0 0 0 1;1 0 1 0 0];
dg = {'node1','node2','node3','node4','node5'};
bg1 = biograph(w,dg)
view(bg1) ; % view the biograph object.
Graphshortestpath
Solve shortest path problem in graph

1.[dist, path, pred] = graphshortestpath(G, S)-


determines the single-source shortest paths from node S to all other nodes in the graph
represented by matrix G.pred contains the predecessor nodes of the winning paths.
2.[dist, path, pred] = graphshortestpath(G, S, T)
determines the single source-single destination shortest path from node S to node T
3.[dist, path, pred] = graphshortestpath(G, S, T, 'Method', MethodValue)
lets us specify the algorithm used to find the shortest path. Choices are:
'Bellman-Ford' Assumes weights of the edges to be nonzero entries in the
N-by-N adjacency matrix. Time complexity is O(N*E), where N and E are the
number of nodes and edges respectively.
'BFS' Breadth-first search. Assumes all weights to be equal, and nonzero
entries in the N-by-N adjacency matrix to represent edges. Time complexity
is O(N+E), where N and E are the number of nodes and edges respectively.
'Acyclic' Assumes the graph represented by the N-by-N adjacency matrix
extracted
from a biograph object, BGObj, to be a directed acyclic graph and that weights
of the edges are nonzero entries in the N-by-N adjacency matrix. Time complexity
is O(N+E), where N and E are the number of nodes and edges respectively.
'Dijkstra' Default algorithm. Assumes weights of the edges to be positive values
in the N-by-N adjacency matrix. Time complexity is O(log(N)*E), where N and E
are
the number of nodes and edges respectively.
Among the options let's see the Dijkstra & Bellman-Ford Algorithms in brief
Dijkstra's Shortest Path Algorithm
One algorithm for finding the shortest path from a starting node to a target node in a
weighted graph is Dijkstras algorithm. The algorithm creates a tree of shortest paths from
the starting vertex, the source, to all other points in the graph.
ijkstras algorithm, published in 1959 and named after its creator Dutch computer scientist
Edsger Dijkstra, can be applied on a weighted graph. The graph can either be directed or
undirected. One stipulation to using the algorithm is that the graph needs to have a
nonnegative weight on every edge.
Dijkstra's Shortest Path Algorithm
One algorithm for finding the shortest path from a starting node to a target node in a
weighted graph is Dijkstras algorithm. The algorithm creates a tree of shortest paths from the
starting vertex, the source, to all other points in the graph.

Dijkstras algorithm, published in 1959 and named after its creator Dutch computer
scientist Edsger Dijkstra, can be applied on a weighted graph. The graph can either be directed
or undirected. One stipulation to using the algorithm is that the graph needs to have a
nonnegative weight on every edge.

Dijkstra's Algorithm
Dijkstras algorithm finds a shortest path tree from a single source node, by building a set
of nodes that have minimum distance from the source.

]
The graph has the following:

vertices, or nodes, denoted in the algorithm by v or u ;


Weighted edges that connect two nodes: (u,v) denotes an edge, and w (u,v) denotes its
weight. In the diagram on the right, the weight for each edge is written in gray.
This is done by initializing three values:

dist ,an array of distances from the source node to each node in the graph, initialized the
following way: dist (s) = 0; and for all other nodes v , dist() = . This is done at the
beginning because as the algorithm proceeds, the from the source to each node in the
graph will be recalculated and finalized when the shortest distance to v is found
Q , a queue of all nodes in the graph. At the end of the algorithm's progress, Q will be
empty.
S, an empty set, to indicate which nodes the algorithm has visited. At the end of the
algorithm's run, S will contain all the nodes of the graph.
The algorithm proceeds as follows:

While Q is not empty, pop the node v , that is not already in S , from Q with the
smallest dist (v) . In the first run, source node will be chosen because dist(s) was
initialized to 0. In the next run, the next node with the smallest dist value is chosen.
Add node v to S , to indicate that has been visited
Update dist values of adjacent nodes of the current node v as follows: for each new
adjacent node u,
if dist(v) + weight < dist (u), there is a new minimal distance found for u, so update dist
(u) to the new minimal distance value;
otherwise, no updates are made to dist (u).

The algorithm has visited all nodes in the graph and found the smallest distance to each
node. dist now contains the shortest path tree from source s.

Bellman-Ford Algorithm

The Bellman-Ford algorithm is a graph search algorithm that finds the shortest path
between a given source vertex and all other vertices in the graph. This algorithm can be used on
both weighted and unweighted graphs.

Like Dijkstra's shortest path algorithm, the Bellman-Ford algorithm is guaranteed to find
the shortest path in a graph. Though it is slower than Dijkstra's algorithm, Bellman-Ford is
capable of handling graphs that contain negative edge weights, so it is more versatile. It is worth
noting that if there exists a negative cycle in the graph, then there is no shortest path. Going
around the negative cycle an infinite number of times would continue to decrease the cost of the
path (even though the path length is increasing). Because of this, Bellman-Ford can also detect
negative cycles which is a useful feature.

Overview

The Bellman-Ford algorithm operates on an input graph, G with |V| vertices


and |E| edges. A single source vertex, , must be provided as well, as the Bellman-Ford algorithm
is a single-source shortest path algorithm. No destination vertex needs to be supplied, however,
because Bellman-Ford calculates the shortest distance to all vertices in the graph from the
source vertex.

The Bellman-Ford algorithm, like Dijkstra's algorithm, uses the principle of relaxation to find
increasingly accurate path length. Bellman-Ford, though, tackles two main issues with this
process.

1. If there are negative weight cycles, the search for a shortest path will go on forever.
2. Choosing a bad ordering for relaxations leads to exponential relaxations.

Matlab code to find shortest path Using Dijkstra Algorithm


%create and view a directed graph with 6 nodes and 11 edges
w = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
dg = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],w);
h = view(biograph(dg,[],'showweights','on'))
%find the shortest path in the graph from node 1 to node 6 using dijkstra algorithim
[dist,path,pred] = graphshortestpath(dg,1,6,'method','dijkstra')
% mark the nodes and edges of the shortest path by coloring them
% red and increasing the line width
set(h.nodes(path),'color',[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.nodes(path),'id'));
set(edges,'linecolor',[1 0 0])
set(edges,'linewidth',1.5)
Matlab code to find shortest path Using 'Bellman-Ford' Algorithm
%Create and view a directed graph with 6 nodes and 11 edges
W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W);
h = view(biograph(DG,[],'ShowWeights','on'))
%Find the shortest path in the graph from node 1 to node 6 using Bellman-Ford
[dist,path,pred] = graphshortestpath(DG,1,6,'Method','Bellman-Ford')
% Mark the nodes and edges of the shortest path by coloring them
%red and increasing the line width
set(h.Nodes(path),'Color',[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)

Вам также может понравиться