Graphs A graph is a data structure that consists of a set of vertices

advertisement
Graphs
A graph is a data structure that consists of a set of vertices and a set of edges
between pairs of vertices
Edges represent paths or connections between the vertices
The set of vertices and the set of edges must both be finite and neither one be
empty
Vertices are represented as points or labeled circles and edges are represented as
lines joining the vertices
The physical layout of the vertices and their labeling are not relevant
Graphs can have one way edges (directed edges like one way streets) or two way
edges.
A graph with directed edges is called a directed graph (digraph)
A graph with undirected edges is an undirected graph or simply a graph
The edges in a graph may have values associated with them known as their
weights
A graph with weighted edges is known as a weighted graph
Vertices are typically labeled with a name or letter. Edges are often referred to as
a pair of vertices.
Edges: AB, BA BE, DA, EA, EC, ED or
E={(A,b), (B,A), (B,E), (D,A), (E,A), (E,C), (E,D)}
Paths and Cycles
Graphs are often used to determine paths between vertices (items)
 A vertex is adjacent to another vertex if there is an edge to it from that
other vertex
 A path is a sequence of vertices in which each successive vertex is adjacent
to its predecessor
 In a simple path, the vertices and edges are distinct except that the first and
last vertex may be the same
 A cycle is a simple path in which only the first and final vertices are the
same
 If a graph is not connected, it is considered unconnected, but will still
consist of connected endpoints.
Examples of things that can be viewed as graphs:
1.
2.
3.
4.
5.
Cities/Roads
Railway system
Prerequisite for classes
Networks
Friendships/relationships
related to, works with, supervises, has class with, etc.
6. A tree is really a special case of a graph!
Often will have data that can be defined as a graph. Will often ask questions:
1.
2.
3.
4.
5.
Does a path exist between point A and point B?
What is the cost (sum of weights) between point A and point F?
Is there a cycle in the path A,B,C…..F?
What is the shortest path from A to G?
Is point A adjacent to point C? (edge exists between them)
Computer representation:
 Adjacency List:
Array of Lists, one list for each vertex storing the vertices
that are adjacent
 Adjacency matrix: use a two-dimensional array representing the graph
o Unweighted graph has Boolean entries
o Weighted graph would contain weights as entries
Java does not provide a graph class!
A graph class should allow you to traverse the graph, move from node to node
along edges
Specification:






Create a graph with n vertices
Iterate through the vertices
Iterate through vertices that are adjacent to a given vertex
Determine whether an edge exists
Determine the weight of an edge
Insert an edge
Breath-First Search can be represented as a Tree!
Depth-First Search
In depth-first search, you start at a vertex, visit it, and choose one adjacent vertex to
visit; then, choose a vertex adjacent to that vertex to visit, and so on until you go no
further; then back up and see whether a new vertex can be found
Graph Traversals
Most graph algorithms involve visiting each vertex in a systematic order
Most common traversal algorithms are the breadth first and depth first search
BreadthFirst
From a given vertex, look at all adjacent vertices, and then all adjacent vertices to them,
etc.
Depth First Search
Breadth First Search
Breadth First Search in effect finds a shortest path from node 1 to node n for an
unweighted graph.
Edsger Dijkstra developed an algorithm to determine the shortest path from anode s to
all other nodes
Two sets:
S (set of vertices done)
V-S (rest of the Vertices)
Two arrays
d[v] (shortest distance from s to v)
p[v] (predecessor vertex in getting to v)
Let w(x,y) be the weight of the edge from x to y
Process
V is the set of all vertices
Put s in S
For all v, initialize d[v] with distance from s if direct edge, else ∞
For all v, initialize p[v] = s
Find the vertex, u in V-S such that d[u] is the smallest
Look at all v adjacent to u. If distance from s to u + distance from u to v is less than d[v],
set d[v] = d[u] + w(u,v) -- distance from u to v
Set p[v] = u
Add u to set S
Repeat until V-S is empty
Step 2: executed v times
Step 7: executed v times
Step 10: v-1 times, v-2 times, v-3 times, ….
We’ve seen this before!!!!
O(n2)
Spanning Trees
Subset of edges so that all nodes are connected but only one path between any set of
nodes ( All trees are spanning trees)
Can drop edges as long as we are still connected
Alternative definition: a spanning tree exists if a graph is connected and dropping any
edge will result in a disconnected graph.
The cost of a spanning tree is the sum of the weights of all the edges
A minimum spanning tree is the spanning tree with the lowest cost
Commonly used by airlines with their hub and spoke systems.
Prim’s Algorithm for minimum spanning tree – very similar to Dijkstra’s Algorithm
Difference: d[v] contains length of the final edge
Download