Professional Documents
Culture Documents
Data structure containing generic interconnected data used to implement directed graph
Description
Property Summary
A biograph object contains two kinds of objects, node objects and edge objects, that have their
own properties.
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
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:
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, 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.