Weighted Graphs:
A Weighted Graph (Weighted DiGraph) is a graph G (a DiGraph G) such
that each edge has associated with it a number indicating a "weight"
attached to that edge. (There is a more general class of graphs called
Edge-Labeled Graphs that simply has a "label" associated with it and
there are also graphs with weights or labels attached to nodes. We will
be less interested in those in this class.)
Weighted graphs are extremely useful. For example, a graph where pairs
are geographical locations and the weights are mileages between those
locations is a useful representation for computing distances between
those locations. Another example is networks where weight might be the
transport time for packets over a particular "cable" between nodes.
There are a number of different problems associated with weighted
graphs. We will start with a fairly simple one:
Given a connected graph, construct a subgraph connecting all nodes in a
graph for which the total weight of the edges is as small as possible.
This is sometimes called the Minimum Spanning Tree problem because it
turns out that the subgraph you get is always a tree. (If not, this
means it has a cycle and, therefore, you can remove an edge without
changing the fact that it connects all the nodes.)
There are two algorithms which solve this problem and both are prime
examples of the algorithmic strategy called greedy algorithm. A greedy algorithm
works by making the best "local" choice in an optimization problem and
continuing this process until you reach a solution. It is optimal if
the solution you reach is as good as possible.
The first algorithm, called Kruskal's algorithm, simply grabs edges in
increasing cost unless they form a cycle.
The second algorithm, called Prim's algorithm, starts by picking a node
and the minimum edge from that node to another node and, then, picking
edges from the nodes already chosing to new nodes which are of minimum
weight.