Directed/Weighted Graphs ♢ COMP2521 ♢ (23T1)

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [0/19]
❖ Generalising Graphs


Discussion so far has considered graphs as

Real-world applications require more "precision" We need to consider directed graphs and weighted graphs
COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [1/19]
❖ Directed Graphs (Digraphs)

Directed graphs are ...

Example digraph:

[Diagram:Pics/digraph0.png]

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [2/19]
❖ ... Directed Graphs (Digraphs)

Some properties of ...

[Diagram:Pics/digraph0.png]

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [3/19]
❖ ... Directed Graphs (Digraphs)


Terminology for digraphs …

Directed path:   sequence of n ≥ 2 vertices v1 → v2 → … → vn

If v1 = vn, we have a directed cycle

Degree of vertex:   number of incident edges

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [4/19]
❖ ... Directed Graphs (Digraphs)


More terminology for digraphs …

Reachability:

Strong connectivity:

Directed acyclic graph (DAG):

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [5/19]
❖ Digraph Representation

Similar set of choices as for undirectional graphs:

V vertices identified by 0 .. V-1

[Diagram:Pics/digraph-rep.png]

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [6/19]
❖ ... Digraph Representation

Example digraph and adjacency matrix representation:

[Diagram:Pics/digraph1.png]

Undirectional symmetric matrix
Directional non-symmetric matrix

Maximum #edges in a digraph with V vertices: V2

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [7/19]
❖ ... Digraph Representation

Costs of representations:    (where degree deg(v) = #edges leaving v)

 array
of edges
adjacency
matrix
adjacency
list
space usageEV2V+E
insert edgeE11
exists edge (v,w)?E1deg(v)
get edges leaving vEVdeg(v)

Overall, adjacency list representation is best

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [8/19]
❖ Weighted Graphs


Graphs so far have considered

Some applications require us to consider
COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [9/19]
❖ ... Weighted Graphs

Weighted graphs are ...

Weights can be used in both directed and undirected graphs.

Example weighted graphs:

[Diagram:Pics/wgraph0.png]

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [10/19]
❖ ... Weighted Graphs

Example: major airline flight routes in Australia

[Diagram:Pics/flights.png]

Representation:   edge = direct flight;   weight = approx flying time (hours)

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [11/19]
❖ ... Weighted Graphs


Weights lead to minimisation-type questions, e.g.

1. Cheapest way to connect all vertices?

2. Cheapest way to get from A to B?
COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [12/19]
❖ Weighted Graph Representation


Weights can easily be added to:

The edge list representation changes to list of (s,t,w) triples

All representations can also work with directed edges


Weight values are determined by domain being modelled

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [13/19]
❖ ... Weighted Graph Representation


Adjacency matrix representation with weights:

[Diagram:Pics/adjmatrixw.png]


Note: need distinguished value to indicate "no edge".

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [14/19]
❖ ... Weighted Graph Representation

Adjacency lists representation with weights:

[Diagram:Pics/adjlistsw.png]

Note: if undirected, each edge appears twice with same weight

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [15/19]
❖ ... Weighted Graph Representation

Edge array / edge list representation with weights:

[Diagram:Pics/edgelistw.png]

Note: not very efficient for use in processing algorithms, but does
give a possible representation for min spanning trees or shortest paths

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [16/19]
❖ Weighted Graph Implementation

Changes to preious grpah data structures to include weights:

WGraph.h

// edges are pairs of vertices (end-points) plus weight
typedef struct Edge {
   Vertex v;
   Vertex w;
   int    weight;
} Edge;

// returns weight, or 0 if vertices not adjacent
int adjacent(Graph, Vertex, Vertex);

Note: here, we assume all weights are positive, but not required

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [17/19]
❖ ... Weighted Graph Implementation


WGraph.c  (assuming adjacency matrix representation)

typedef struct GraphRep {
   int **edges;  // adjacency matrix storing weights
                 // 0 if nodes not adjacent
   int   nV;     // #vertices
   int   nE;     // #edges
} GraphRep;

bool adjacent(Graph g, Vertex v, Vertex w) {
   assert(valid graph, valid vertices)
   return (g->edges[v][w] != 0);
}

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [18/19]
❖ ... Weighted Graph Implementation

More  WGraph.c

void insertEdge(Graph g, Edge e) {
   assert(valid graph, valid edge)
   // edge e not already in graph
   if (g->edges[e.v][e.w] == 0) g->nE++;
   // may change weight of existing edge
   g->edges[e.v][e.w] = e.weight;
   g->edges[e.w][e.v] = e.weight;
}

void removeEdge(Graph g, Edge e) {
   assert(valid graph, valid edge)
   // edge e not in graph
   if (g->edges[e.v][e.w] == 0) return;
   g->edges[e.v][e.w] = 0;
   g->edges[e.w][e.v] = 0;
   g->nE--;
}

COMP2521 (23T1) ♢ Directed/Weighted Graphs ♢ [19/19]


Produced: 26 Mar 2023