❖ Generalising Graphs |
Discussion so far has considered graphs as
❖ Directed Graphs (Digraphs) |
Directed graphs are ...
❖ ... Directed Graphs (Digraphs) |
Some properties of ...
❖ ... Directed Graphs (Digraphs) |
Terminology for digraphs …
Directed path: sequence of n ≥ 2 vertices v1 → v2 → … → vn
Degree of vertex: number of incident edges
❖ ... Directed Graphs (Digraphs) |
More terminology for digraphs …
Reachability:
Strong connectivity:
Directed acyclic graph (DAG):
❖ Digraph Representation |
Similar set of choices as for undirectional graphs:
❖ ... Digraph Representation |
Example digraph and adjacency matrix representation:
Undirectional ⇒ symmetric matrix
Directional ⇒ non-symmetric matrix
Maximum #edges in a digraph with V vertices: V2
❖ ... Digraph Representation |
Costs of representations: (where degree deg(v) = #edges leaving v)
array of edges | adjacency matrix | adjacency list | |
space usage | E | V2 | V+E |
insert edge | E | 1 | 1 |
exists edge (v,w)? | E | 1 | deg(v) |
get edges leaving v | E | V | deg(v) |
Overall, adjacency list representation is best
❖ Weighted Graphs |
Graphs so far have considered
❖ ... Weighted Graphs |
Weighted graphs are ...
Example weighted graphs:
❖ ... Weighted Graphs |
Example: major airline flight routes in Australia
Representation: edge = direct flight; weight = approx flying time (hours)
❖ ... Weighted Graphs |
Weights lead to minimisation-type questions, e.g.
1. Cheapest way to connect all vertices?
❖ Weighted Graph Representation |
Weights can easily be added to:
All representations can also work with directed edges
Weight values are determined by domain being modelled
❖ ... Weighted Graph Representation |
Adjacency matrix representation with weights:
Note: need distinguished value to indicate "no edge".
❖ ... Weighted Graph Representation |
Adjacency lists representation with weights:
Note: if undirected, each edge appears twice with same weight
❖ ... Weighted Graph Representation |
Edge array / edge list representation with weights:
Note: not very efficient for use in processing algorithms, but does
give a possible representation for min spanning trees or shortest paths
❖ 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
❖ ... Weighted Graph Implementation |
WGraph.c
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); }
❖ ... 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--; }
Produced: 26 Mar 2023