Graphs The ultimate data structure - Kirkwood …graphs 2 Definition of graph • Non-linear data...
Transcript of Graphs The ultimate data structure - Kirkwood …graphs 2 Definition of graph • Non-linear data...
graphs 1
Graphs
The ultimate data structure
graphs 2
Definition of graph
• Non-linear data structure consisting of nodes & links between them (like trees in this sense)
• Unlike trees, graph nodes may be completely unordered, or may be linked in any order suited to the particular application
graphs 3
Undirected graphs
• Simplest form of graph• Set of nodes (vertices -- one node is a vertex)
and set of links between them (edges)• Either or both sets may be empty• Each edge connects two vertices; as the name
implies, neither vertex is considered to be a point of origin or a point of destination
• An edge may also connect a vertex to itself
graphs 4
Example graph
v1v0
v4
v2
v3
v1
v0
v2
v3
v4
e0 e0
e1 e1
e2
e2e4
e4
This graph has 5 vertices and4 edges; a graph is defined bywhich vertices are connected, and which edges connect them
This is the same graph,even though its shape isdifferent
graphs 5
Application: an undirected state graph
• Each vertex represents the state of some object or situation
• Each edge represents the possibility of moving directly from one state to another
• There exists a path from one state to another if there is an edge connecting each vertex and any vertices between them
graphs 6
Undirected state graph example
graphs 7
Directed graphs
• Each edge has an orientation, or direction• One vertex connected by the edge is the source, while the other is the target
• Directed graphs are diagrammed like undirected graphs, except the edges are represented by arrows instead of lines
graphs 8
Directed graph example
a
c
d
e
b
f
Each arrow representsthe direction of theedge; to get frompoint a to pointf, you must passthrough c, d and b(in that order), butto get from f to arequires only goingthrough b (by the shortest route)
graphs 9
More graph terminology
• Loop: an edge that connects a vertex to itself; in the illustration, edge e3 is a loop
• Multiple edges: two or more edges connecting to vertices in the same direction– e4 and e5 are multiple edges– e1 and e2 are not
• Simple graph: a graph with no loops or multiple edges
graphs 10
Directed graph application examples
• Route diagrams for airline routes• Course pre- and co-requisites• Program flow charts• Programming language syntax diagrams
11
Modeling with graphs - examples
• Niche overlap graph in ecology:– each species represented by vertex– undirected edge connects 2 vertices if the
species compete• Influence graph:
– each person in group is represented by vertex– directed edge from vertex a to vertex b when a
has influence on b
12
Modeling with graphs - examples
• Round-robin tournament graph:– each team represented by vertex– (a,b) is an edge if team a beats team b
• Precedence graph:– vertices represent statements in a computer
program– directed edge between vertices means 1st
statement must be executed before 2nd
13
Terms related to undirected graphs
• Adjacent: 2 vertices u & v in an undirected graph G are adjacent (neighbors) in G if there is an edge {u,v}
• Incident, connect: if edge e = {u,v}, e is incident with vertices u & v, and e connects u and v
• Endpoints: vertices u & v are endpoints of edge e
14
Terms related to undirected graphs
• Degree of vertex in an undirected graph is the number of edges incident with it– loops count twice– degree of vertex v is denoted deg(v)
• Example: what are the degrees of the vertices in this graph?
15
Terms related to undirected graphs
• Isolated vertex (like e in previous example) has degree 0, not adjacent to any other vertex
• Pendant vertex (like d in previous example) - adjacent to exactly one vertex
• The sum of the degrees of all vertices in a graph is exactly twice the number of edges in the graph
16
Handshaking Theorem
• Let G=(V,E) be an undirected graph with e edges; then 2e = ∑
∈Vvv)deg(
• Note that sum of degrees of vertices is always even; this leads to the theorem:
An undirected graph has an even number of vertices of odd degree
17
Terminology related to directed graphs
• Let G be a directed graph, with an edge e=(u,v):– u is initial vertex– v is terminal, or end vertex– u is adjacent TO v– v is adjacent FROM u– initial & terminal vertex of a loop are the same
18
Terminology related to digraphs
• In-degree of vertex v, denoted deg-(v), is the number of edges with v as the terminal vertex
• Out-degree of vertex v, denoted deg+(v), is the number of edges with v as the initial vertex
• A loop has one of each
19
Terminology related to digraphs
• The sum of in-degrees is equal to the sum of out-degrees
• Both are equal to the number of edges in the graph:
– Let G=(V,E) be a directed graph; then
∑∑∈∈
=+=−VvVv
vv |E|)(deg)(deg
• The undirected graph that results from ignoring arrows in a directed graph is called the underlying undirected graph
20
Classes of simple graphs
• Complete graphs: complete graph on n vertices, denoted Kn, is the simple graph that contains exactly one edge between each pair of distinct vertices; Examples:
21
Classes of Simple Graphs
• Cycle: Cn, where n 3, consists of n vertices v1, v2, … , vn and edges {v1, v2}, {v2, v3}, … , {vn-1, vn} Examples:
22
Classes of Simple Graphs
• Wheel: a cycle with an additional vertex, which is adjacent to all other vertices; example:
23
Classes of Simple Graphs
• n-Cube: denoted Qn, is a graph representing the 2n bit strings of length n:
• 2 vertices are adjacent if and only if the bit strings they represent differ in exactly one position. Examples:
24
Bipartite Graphs
• A simple graph G is called bipartite if its vertex set V can be partitioned into 2 disjoint non-empty sets V1 and V2 such that:– every edge connects a vertex in V1 with a
vertex in V2– no edge connects 2 vertices in V1 or in V2
25
Example: lining up 1st graders
Cate JohnMary MarkMary Ann TerryMary Pat TimMarie Jimmy
26
Example - cycle
• C6 is bipartite; can partition its vertex set into 2 distinct sets:– V1 = {v1, v3, v5)– V2 = {v2, v4, v6}– with every edge connecting a
vertex in V1 with one in V2
27
ExamplesGraph at left is not bipartite; to divide into2 sets, one set must include 2 vertices and tobe bipartite, those vertices must not be connectedBut every vertex in this graph is connected to2 others
This graph, on the other hand, is bipartite; thetwo sets are V1 = {v1, v3, v5} and V2 = {v2, v4, v6}Note that the definition doesn’t say a vertex inone set can’t connect to more than one vertex inthe other - only that each in one must connect to one in the other, and no vertex in a set can connectto a vertex in the same set
28
Examples: are they bipartite?
29
Complete Bipartite Graphs
• Denoted Km,n is the graph that has its vertex set partitioned into 2 subsets of m vertices and n vertices
• There is an edge between 2 vertices if and only if one vertex is in the first subset and the other is in the second subset
30
Examples
31
Applications of Special Types of Graphs: LAN topology
• A star is a complete bipartite K1,n graph:
• A ring is an n-cycle:
• A redundant network may have both a central hub and a ring, forming a wheel:
32
Subgraph
• Graph obtained by removing vertices and their associated edges from a larger graph; more formally:
• Subgraph of G=(V,E) is H=(W,F) where W V and F E
33
Subgraph Example
34
Can combine graphs, forming a union
• Let G1 = (V1, E1) and G2 = (V2, E2)• The union, G1 G2 = (V1 V2, E1
E2)
graphs 35
Graph implementations
• All of the following representations can be used to create an instance of a directed graph in which loops are allowed:– adjacency list– adjacency matrix – edge lists– edge sets– incidence matrices
36
Representing Graphs
• Adjacency list: specifies vertices adjacent with each vertex in a simple graph– can be used for directed graph as well - list
terminal vertices adjacent from each vertexVertex Adjacent vertices a c,d b e c a,d d a,c,e e d,b
graphs 37
Adjacency matrix• Square grid of true/false values that represent
the edges of a graph• If the intersection of a row and column has a
true value in it, there exists an edge between the vertices represented by the two indexes
• Since this is a directed graph, the value will be true only if there is an edge from the vertex indicated by the row index to the vertex indicated by the column index
graphs 38
Adjacency matrix
• An adjacency matrix can be stored in a two-dimensional array of bools or ints
• Each vertex is represented by one row and one column
• For a graph with four vertices, the following declaration could be used:
boolean [][] aMatrix = new boolean [4][4];
graphs 39
Adjacency matrix
[0] [1] [2] [3]
[0]
[1]
[2]
[3]
row
column
The graph above could berepresented by the adjacencymatrix on the right
f t f f
f f f t
f f f f
f t t t
40
Representing Graphs: mathematical POV
• Adjacency matrix: n x n zero-one matrix with 1 representing adjacency, 0 representing non-adjacency
• Adjacency matrix of a graph is based on chosen ordering of vertices; for a graph with n vertices, there are n! different adjacency matrices
• Adjacency matrix of graph with few edges is a sparse matrix - many 0s, few 1s
41
Simple graph adjacency matrix
With ordering a,b,c,d,e:0 0 1 1 00 0 0 0 11 0 0 1 01 0 1 0 10 1 0 1 0
42
Using adjacency matrices for multigraphs and pseudographs
• With multiple edges, matrix is no longer zero-one: if more than one edge exists, list the number of edges
• A loop is represented with a 1 in position i,i• Adjacency matrices for simple graphs,
including multigraphs and pseudographs, are symmetric
43
Adjacency matrices and digraphs
• For directed graphs, the adjacency matrix has a 1 in its (i,j)th position if there is an edge from vi to vj
• Directed multigraphs can be represented by placing the number of edges in in position i,j that exist from vi to vj
• Adjacency matrices for digraphs are not necessarily symmetric
graphs 44
Edge lists
• Linked lists are used to represent vertices• Each vertex is a linked list; nodes in each
list contain numbers indicating the target vertices for that particular source vertex
• In other words, if list A contains a link to vertex B, there is an edge from A to B
graphs 45
Edge lists
• For n vertices, there must be n lists; often the lists are stored as an array of list head references
• A vertex that is not a source would be represented in the array by a NULL pointer
graphs 46
Edge lists
The graph above could berepesented by the array of edgelists on the right
[0] [1] [2] [3]
1
null
3
null
null
null
1
3
2
graphs 47
Edge Sets
• Requires a previously programmed implementation of a set ADT
• Properties of a set:– contains an unordered collection of entries– each item is unique– operations include insertion, deletion, and
checking for the presence of a specific item• To represent a graph with 4 vertices, can
declare an array of 4 sets of ints
48
Incidence Matrices
• In an adjacency matrix, both rows and columns represent vertices
• In an incidence matrix, rows represent vertices while columns represent edges– A 1 in any matrix position means the edge in
that column is incident with the vertex in that row
– Multiple edges are represented identical columns depicting the edges
49
Incidence Matrix Example
e1 e2 e3 e4 e5 e6 e7 e8 e9a 1 1 1 1 0 0 0 0 0b 0 0 0 0 1 1 0 0 0c 0 1 0 0 0 0 1 1 0d 0 0 0 1 0 0 0 1 1e 1 0 1 0 0 1 0 0 1
graphs 50
Choosing an implementation
• Adjacency matrix is easier to implement & use than any of the edge-based solutions
• Frequency of certain operations may dictate another solution
graphs 51
Choosing an implementation
• Both adding and removing edges and checking for the presence of a particular edge are O(N) operations in the worst case for edge lists; may be as low as O(logN) for edge sets, depending on representation
• Edge lists are very efficient for operations that execute one time for each edge with a particular vertex
graphs 52
Disadvantages of adjacency matrix
• Operations on all edges for a vertex require stepping through entire row (whether or not an entry is an edge) -- this is O(N)
• If each vertex has very few edges, an adjacency matrix is mostly wasted space
graphs 53
Simple graph ADT implementation: labeled graph
• Operations:– constructor– add/removeEdge– size: returns number of vertices in graph– isEdge: returns true if edge exists between specified
source & target– clone– get/setLabel– neighbors: returns array of neighbors of a specified
vertex
graphs 54
Simple graph ADT implementation: labeled graph
• Data components:– 2-dimensional int array representing edges– array of vertex labels: each vertex includes data
consisting of an informational label
graphs 55
Labeled graph class definition
public class Graph implements Cloneable{ private boolean[ ][ ] edges; private Object[ ] labels;
public Graph(int n) // n: # of vertices { edges = new boolean[n][n]; // All values initially false labels = new Object[n]; // All values initially null }
graphs 56
Labeled graph class definition
public void addEdge(int source, int target) { edges[source][target] = true; }
public void removeEdge(int source, int target) { edges[source][target] = false;}
public boolean isEdge(int source, int target) { return edges[source][target];}
graphs 57
Labeled graph class definition
public Object getLabel(int vertex) { return labels[vertex];}
public void setLabel(int vertex, Object newLabel) { labels[vertex] = newLabel;}
public int size( ) { return labels.length;}
graphs 58
Labeled graph class definitionpublic int[ ] neighbors(int vertex) { int i , count=0; int[ ] answer; // First count how many edges have the vertex as their source for (i = 0; i < labels.length; i++) { if (edges[vertex][i]) count++; } // Allocate the array for the answer answer = new int[count];
graphs 59
Labeled graph class definition
// Fill the array for the answer count = 0; for (i = 0; i < labels.length; i++) { if (edges[vertex][i]) answer[count++] = i; } return answer; }
graphs 60
Labeled graph class definitionpublic Object clone( ) { // Clone a Graph object. Graph answer; try { answer = (Graph) super.clone( ); } catch (CloneNotSupportedException e) { // This would indicate an internal error in the Java runtime system // because super.clone always works for an Object. throw new InternalError(e.toString( )); } answer.edges = (boolean [ ][ ]) edges.clone( ); answer.labels = (Object [ ]) labels.clone( ); return answer; }
graphs2 61
Graph traversals
• Traversal: visiting all nodes in a data structure
• Can’t apply tree traversal methods to graphs, since nodes in a graph don’t have the same child/parent relationships
• Graph traversal methods utilize subsidiary data structures (queues and stacks) to keep track of vertices to be visited
graphs2 62
Graph traversals
• Purpose of traversal algorithm: starting at a given vertex, process the information at that vertex, then move along an edge to process a neighbor
• When traversal is complete, all vertices that can be reached from the start vertex (but not necessarily all vertices in the graph) have been visited
graphs2 63
Depth-first search
• After processing the initial vertex, a depth-first search moves along an edge to one of the vertex’s neighbors
• Once the second vertex is processed, the search moves along an edge to one of its neighbors
• The process continues to move forward as long as vertices can be reached in this manner
graphs2 64
Depth-first search
• The search always proceeds forward until no further forward moves can be made
• Once forward motion is no longer possible at the current node, the search backs up to the previous vertex and visits any previously unvisited neighbors
• Eventually the search backs up to the original starting node, having visited all nodes that were reachable from this point
graphs2 65
Depth-first search example
v1v2
v3
v4
v5
v0
Depth-first printing routine
graphs 66
public static void depthFirstPrint(Graph g, int start) { boolean[ ] marked = new boolean [g.size( )]; depthFirstRecurse(g, start, marked); }
// The part of a stack is played here by the system stack;// the depthFirstRecurse method stacks up activation records// with each call
Recursive method for depth-first printing
graphs 67
public static void depthFirstRecurse(Graph g, int v, boolean[ ] marked) { int[ ] connections = g.neighbors(v); int i, nextNeighbor; marked[v] = true; System.out.println(g.getLabel(v)); // Traverse all the neighbors, looking for unmarked vertices: for (i = 0; i < connections.length; i++) { nextNeighbor = connections[i]; if (!marked[nextNeighbor]) depthFirstRecurse(g, nextNeighbor, marked); } }
graphs2 68
Breadth-first search
• Uses queue to keep track of vertices which might still have unprocessed neighbors
• Initial vertex is processed, marked, and placed in queue
• Repeats the following until queue is empty:– remove vertex from front of queue– for each unmarked neighbor of the current
vertex, mark the neighbor, then place the neighbor on the queue
graphs2 69
Breadth-first search example
v1v2
v3
v4
v5
v0
Queue of visited nodes
v0v1v2v2 v4 v4
70
Graph Isomorphism
• The simple graphs G1=(V1,E1) and G2=(V2,E2) are isomorphic if there is a one-to-one and onto function f from V1 to V2 with vertices a and b adjacent in G1 if and only if f(a) and f(b) are adjacent in G2 for all vertices a and b in G1
• Function f is called an isomorphism
71
Determining Isomorphism
• The number of vertices, number of edges, and degrees of the vertices are invariants under isomorphism
• If any of these quantities differ in two simple graphs, the graphs are not isomorphic
• If these quantities are the same, the graphs may or may not be isomorphic
72
Determining Isomorphism
• To show that a function f from the vertex set of graph G to the vertex set of graph H is an isomorphism, we need to show that f preserves edges
• Adjacency matrices can help with this: we can show that the adjacency matrix of G is the same as the adjacency matrix of H when rows and columns are arranged according to f
73
ExampleConsider the two graphs below:
• Both have six vertices and seven edges;• Both have four vertices of degree 2 and two vertices of degree 3;• The subgraphs of G and H consisting of vertices of degree 2 and the edges connecting them are isomorphic
74
Example continued
• Need to define a function f and then determine whether or not it is an isomorphism:
– deg(u1) in G is 2; u1 is not adjacent to any other degree 2 vertex, so the image of u1 must be either v4 or v6 in H
– can arbitrarily set f(u1) = v6 (if that doesn’t work, we can try v4)
75
Example continued
• Continuing definition of f:– Since u2 is adjacent to u1, the possible images
of u2 are v3 and v5– We set f(u2) = v3 (again, arbitrarily)
• Continuing in this fashion, using adjacency and degree of vertices as guides, we derive f for all vertices in G, as shown on next slide
76
Example continued
f(u1) = v6f(u2) = v3f(u3) = v4f(u4) = v5f(u5) = v1f(u6) = v2
Next, we set up adjacency matrices for G and H, arranging the matrix for H so that the images of G’s vertices line upwith their corresponding vertices …
77
Example continued
u1 u2 u3 u4 u5 u6u1 0 1 0 1 0 0u2 1 0 1 0 0 1u3 0 1 0 1 0 0u4 1 0 1 0 1 0u5 0 0 0 1 0 1u6 0 1 0 0 1 0
AG
v6 v3 v4 v5 v1 v2v6 0 1 0 1 0 0v3 1 0 1 0 0 1v4 0 1 0 1 0 0v5 1 0 1 0 1 0v1 0 0 0 1 0 1v2 0 1 0 0 1 0
AH
78
Example concluded
• Since AG = AH, it follows that f preserves edges, and we conclude that f is an isomorphism; thus G and H are isomorphic
• If f were not an isomorphism, we would not have proved that G and H are not isomorphic, since another function might show isomorphism