9024 week6 伪代码
伪代码
Array-of-edges Representation
Graph initialisation
newGraph(V):
| Input number of nodes V
| Output new empty graph
|
| g.nV = V // #vertices (numbered 0..V-1)
| g.nE = 0 // #edges
| allocate enough memory for g.edges[]
| return g
How much is enough? … No more than V(V-1)/2 … Much less in practice (sparse graph)
Edge insertion
insertEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| g.edges[g.nE]=(v,w)
| g.nE=g.nE+1
| g.nE=g.nE-1
Exercise #3: Array-of-edges Representation 27/83
Assuming an array-of-edges representation …
Write an algorithm to output all edges of the graph
answer:
show(g):
| Input graph g
|
| for all i=0 to g.nE-1 do
| print g.edges[i]
| end for
Time complexity: O(E)
Adjacency Matrix Representation
Graph initialisation
newGraph(V):
| Input number of nodes V
| Output new empty graph
|
| g.nV = V // #vertices (numbered 0..V-1)
| g.nE = 0 // #edges
| allocate memory for g.edges[][]
| for all i,j=0..V-1 do
| g.edges[i][j]=0 // false
| end for
| return g
Edge insertion
insertEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| if g.edges[v][w]=0 then // (v,w) not in graph
| g.edges[v][w]=1 // set to true
| g.edges[w][v]=1
| g.nE=g.nE+1
| end if
Edge removal
removeEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| if g.edges[v][w]≠0 then // (v,w) in graph
| g.edges[v][w]=0 // set to false
| g.edges[w][v]=0
| g.nE=g.nE-1
| end if
Exercise #4: Show Graph
Assuming an adjacency matrix representation ...
Write an algorithm to output all edges of the graph (no duplicates)
show(g):
| Input graph g
|
| for all i=0 to g.nV-2 do
| | for all j=i+1 to g.nV-1 do | | if g.edges[i][j] then
| | print i"—"j
| | end if
| | end for
| end for
Time complexity:
Exercise #5:
Analyse storage cost and time complexity of adjacency matrix representation
If the graph is sparse, most storage is wasted.
Cost of operations:
initialisation: (initialise V×V matrix)
insert edge: (set two cells in matrix)
delete edge: (unset two cells in matrix)
Adjacency List Representation
Graph initialisation
newGraph(V):
| Input number of nodes V
| Output new empty graph
|
| g.nV = V // #vertices (numbered 0..V-1)
| g.nE = 0 // #edges
| allocate memory for g.edges[]
| for all i=0..V-1 do
| g.edges[i]=NULL // empty list
| end for
| return g
Edge insertion:
insertEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| insertLL(g.edges[v],w)
| insertLL(g.edges[w],v)
| g.nE=g.nE+1
Edge removal:
removeEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| deleteLL(g.edges[v],w)
| deleteLL(g.edges[w],v)
| g.nE=g.nE-1
Exercise #7: Graph ADT Client
Write a program that uses the graph ADT to build the graph depicted below
50/83
print all the nodes that are incident to vertex 1 in ascending order
#include <stdio.h>
#include "Graph.h"
#define NODES 4
#define NODE_OF_INTEREST 1
int main(void) {
Graph g = newGraph(NODES);
Edge e;
e.v = 0; e.w = 1; insertEdge(g,e);
e.v = 0; e.w = 3; insertEdge(g,e);
e.v = 1; e.w = 3; insertEdge(g,e);
e.v = 3; e.w = 2; insertEdge(g,e);
int v;
for (v = 0; v < NODES; v++) {
if (adjacent(g, v, NODE_OF_INTEREST))
printf("%d\n", v);
}
freeGraph(g);
return 0; }
Graph ADT (Array of Edges)
Implementation of GraphRep (array-of-edges representation)
typedef struct GraphRep {
Edge *edges; // array of edges
int nV;// #vertices (numbered 0..nV-1)
int nE;// #edges
int n;// size of edge array
} GraphRep;
Graph ADT (Adjacency Matrix)
Implementation of GraphRep (adjacency-matrix representation)
typedef struct GraphRep {
int **edges; // adjacency matrix
int nV; // #vertices
int nE; // #edges
} GraphRep;
Implementation of graph initialisation (adjacency-matrix representation)
Graph newGraph(int V) {
assert(V >= 0);
int i;
Graph g = malloc(sizeof(GraphRep));
g->nV = V; g->nE = 0;
// allocate memory for each row
g->edges = malloc(V * sizeof(int *));
// allocate memory for each column and initialise with 0
for (i = 0; i < V; i++) {
g->edges[i] = calloc(V, sizeof(int));
assert(g->edges[i] != NULL);
}
return g;
}
standard library function calloc(size_t nelems, size_t nbytes) allocates a memory block of size nelems*nbytes and sets all bytes in that block to zero
Implementation of edge insertion/removal (adjacency-matrix representation)
// check if vertex is valid in a graph
bool validV(Graph g, Vertex v) {
return (g != NULL && v >= 0 && v < g->nV);
}
void insertEdge(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (!g->edges[e.v][e.w]) { // edge e not in graph
g->edges[e.v][e.w] = 1;
g->edges[e.w][e.v] = 1;
g->nE++;
} }
void removeEdge(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (g->edges[e.v][e.w]) { // edge e in graph
g->edges[e.v][e.w] = 0;
g->edges[e.w][e.v] = 0;
g->nE--;
} }
Exercise #8: Checking Neighbours (i)
Graph ADT (Adjacency List)
Implementation of GraphRep (adjacency-list representation)
typedef struct GraphRep {
Node **edges; // array of lists
int nV; // #vertices
int nE; // #edges
} GraphRep;
typedef struct Node {
Vertex v;
struct Node *next;
} Node;
Exercise #9: Checking Neighbours (ii)
Depth-first Search
Recursive DFS path checking
hasPath(G,src,dest):
| Input graph G, vertices src,dest
| Output true if there is a path from src to dest in G, | false otherwise
|
| return dfsPathCheck(G,src,dest)
dfsPathCheck(G,v,dest):
| mark v as visited
| if v=dest then // found dest
| return true
| else
| | for all (v,w)∈edges(G) do
| | | if w has not been visited then
| | | return dfsPathCheck(G,w,dest) // found path via w to dest | | | end if
| | end for
| end if
| return false // no path from v to dest
visited[] // store previously visited node, for each vertex 0..nV-1
findPath(G,src,dest):
| Input graph G, vertices src,dest
|
| for all vertices v∈G do
| visited[v]=-1
| end for
| visited[src]=src
| if dfsPathCheck(G,src,dest) then // show path in dest..src order | | v=dest
// found edge from v to dest
| |
| |
| |
| | end while | | print src | end if
while v≠src do print v"-"
v=visited[v]
dfsPathCheck(G,v,dest):
| if v=dest then // found edge from v to dest
| return true
| else
| | for all (v,w)∈edges(G) do | | | if visited[w]=-1 then | | | | visited[w]=v
| | | | if dfsPathCheck(G,w,dest) then
| | | | return true | | | | end if
| | | end if
| | end for
| end if
| return false // no path from v to dest
DFS can also be described non-recursively (via a stack):
hasPath(G,src,dest):
| Input graph G, vertices src,dest
| Output true if there is a path from src to dest in G,
| false otherwise
|
| push src onto new stack s
| found=false
| while not found and s is not empty do
| | pop v from s
| | mark v as visited
| | if v=dest then
| | found=true
| | else
| | | for each (v,w)∈edges(G) such that w has not been visited
| | | push w onto s | | | end for
| | end if
| end while
| return found
Uses standard stack operations (push, pop, check if empty)
Time complexity is the same: O(V+E) (each vertex added to stack once, each element in vertex's adjacency list visited once)
Breadth-first Search
visited[] // array of visiting orders, indexed by vertex 0..nV-1
findPathBFS(G,src,dest):
| Input graph G, vertices src,dest
|
| for all vertices v∈G do
| visited[v]=-1
| end for
| enqueue src into new queue q
| visited[src]=src
| found=false
| while not found and q is not empty do
| | dequeue v from q
| | if v=dest then
| | found=true
| | else
| | | for each (v,w)∈edges(G) such that visited[w]=-1 do enqueue w into q
| | | visited[w]=v
| | | end for
| | end if
| end while
| if found then
| display path in dest..src order | end if