Graph Data Structure
Graph Data Structure
Sort Yourselves
Sort yourselves in alphabetical order so I can return the midterms efciently!
Graphs
Graphs are one of the unifying themes of computer science. A graph G = (V, E) is dened by a set of vertices V , and a set of edges E consisting of ordered or unordered pairs of vertices from V .
Road Networks
In modeling a road network, the vertices may represent the cities or junctions, certain pairs of which are connected by roads/edges.
Stony Brook Green Port Orient Point vertices - cities Riverhead edges - roads Shelter Island
Electronic Circuits
In an electronic circuit, with junctions as vertices as components as edges.
vertices: junctions edges: components
Flavors of Graphs
The rst step in any graph problem is determining which avor of graph you are dealing with. Learning to talk the talk is an important part of walking the walk. The avor of graph has a big impact on which algorithms are appropriate and efcient.
undirected
directed
Road networks between cities are typically undirected. Street networks within cities are almost always directed because of one-way streets. Most graphs of graph-theoretic interest are undirected.
The edges of a road network graph might be weighted with their length, drive-time or speed limit. In unweighted graphs, there is no cost distinction between various edges and vertices.
simple
nonsimple
sparse
dense
Graphs are usually sparse due to application-specic constraints. Road networks must be sparse because of road junctions. Typically dense graphs have a quadratic number of edges while sparse graphs are linear in size.
cyclic
acyclic
Directed acyclic graphs are called DAGs. They arise naturally in scheduling problems, where a directed edge (x, y) indicates that x must occur before y.
explicit
implicit
embedded
topological
Example: TSP or Shortest path on points in the plane. Example: Grid graphs. Example: Planar graphs.
G unlabeled labeled
An important graph problem is isomorphism testing, determining whether the topological structure of two graphs are in fact identical if we ignore any labels.
George Bush
Nancy Reagan
Saddam Hussain
This graph is well-dened on any set of people: SUNY SB, New York, or the world. What questions might we ask about the friendship graph?
Am I my own friend?
An edge of the form (x, x) is said to be a loop. If x is ys friend several times over, that could be modeled using multiedges, multiple edges between the same pair of vertices. A graph is said to be simple if it contains no loops and multiple edges.
Adjacency Lists
An adjacency list consists of a N 1 array of pointers, where the ith element points to a linked list of the edges incident on vertex i.
1 2
1 2 1 2 2 4 3 5 4 5 1 3 2 3 4
3 5 4
2 3 4 5
To test if edge (i, j) is in the graph, we search the ith list for j, which takes O(di ), where di is the degree of the ith vertex. Note that di can be much less than n when the graph is sparse. If necessary, the two copies of each edge can be linked by a pointer to facilitate deletions.
Both representations are very useful and have different properties, although adjacency lists are probably better for most problems.
Edge Representation
typedef struct { edgenode *edges[MAXV+1]; int degree[MAXV+1]; int nvertices; int nedges; bool directed; } graph;
The degree eld counts the number of meaningful entries for the given vertex. An undirected edge (x, y) appears twice in any adjacency-based graph structure, once as y in xs list, and once as x in ys list.
Initializing a Graph
initialize graph(graph *g, bool directed) { int i; g > nvertices = 0; g > nedges = 0; g > directed = directed; for (i=1; i<=MAXV; i++) g >degree[i] = 0; for (i=1; i<=MAXV; i++) g >edges[i] = NULL; }
Reading a Graph
A typical graph format consists of an initial line featuring the number of vertices and edges in the graph, followed by a listing of the edges at one vertex pair per line.
read graph(graph *g, bool directed) { int i; int m; int x, y; initialize graph(g, directed); scanf(%d %d,&(g >nvertices),&m); for (i=1; i<=m; i++) { scanf(%d %d,&x,&y); insert edge(g,x,y,directed); } }
Inserting an Edge
insert edge(graph *g, int x, int y, bool directed) { edgenode *p; p = malloc(sizeof(edgenode)); p >weight = NULL; p >y = y; p >next = g >edges[x]; g >edges[x] = p; g >degree[x] ++; if (directed == FALSE) insert edge(g,y,x,TRUE); else g >nedges ++; }