PSU CS 311 - Algorithms Analysis and Design Graphs.

Presentation on theme: "PSU CS 311 - Algorithms Analysis and Design Graphs."— Presentation transcript:

PSU CS 311 - Algorithms Analysis and Design Graphs

PSU CS 311 - Algorithms Analysis and Design 2 Graph definitions n There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Nantes Paris Lyon Grenoble Bordeaux Marseille Monaco 400 340 590 420 350 120 320 510 An undirected graph start fill pan with water take egg from fridge break egg into pan boil water add salt to water A directed graph

PSU CS 311 - Algorithms Analysis and Design 3 Graph terminology I n A graph is a collection of nodes (or vertices, singular is vertex) and edges (or arcs) l Each node contains an element l Each edge connects two nodes together (or possibly the same node to itself) and may contain an edge attribute n A directed graph is one in which the edges have a direction n An undirected graph is one in which the edges do not have a direction l Note: Whether a graph is directed or undirected is a logical distinction—it describes how we think about the graph l Depending on the implementation, we may or may not be able to follow a directed edge in the “backwards” direction

PSU CS 311 - Algorithms Analysis and Design 4 Graph terminology II n The size of a graph is the number of nodes in it n The empty graph has size zero (no nodes) n If two nodes are connected by an edge, they are neighbors (and the nodes are adjacent to each other) n The degree of a node is the number of edges it has n For directed graphs, l If a directed edge goes from node S to node D, we call S the source and D the destination of the edge –The edge is an out-edge of S and an in-edge of D –S is a predecessor of D, and D is a successor of S l The in-degree of a node is the number of in-edges it has l The out-degree of a node is the number of out-edges it has

PSU CS 311 - Algorithms Analysis and Design 5 Graph terminology III n A path is a list of edges such that each node (but the last) is the predecessor of the next node in the list n A cycle is a path whose first and last nodes are the same n Example: (Paris, Nantes, Bordeaux, Lyon) is a path n Example: (Paris, Nantes, Lyon, Paris) is a cycle n A cyclic graph contains at least one cycle n An acyclic graph does not contain any cycles Nantes Paris Lyon Grenoble Bordeaux Marseille Monaco 400 340 590 420 350 120 320 510 An undirected graph

PSU CS 311 - Algorithms Analysis and Design 6 Graph terminology IV n An undirected graph is connected if there is a path from every node to every other node n A directed graph is strongly connected if there is a path from every node to every other node n A directed graph is weakly connected if the underlying undirected graph is connected n Node X is reachable from node Y if there is a path from Y to X n A subset of the nodes of the graph is a connected component (or just a component) if there is a path from every node in the subset to every other node in the subset

PSU CS 311 - Algorithms Analysis and Design 7 Adjacency-matrix representation I n One simple way of representing a graph is the adjacency matrix n A 2-D array has a mark at [i][j] if there is an edge from node i to node j n The adjacency matrix is symmetric about the main diagonal A B G E F D C ABCDEFGABCDEFG A B C D E F G This representation is only suitable for small graphs! (Why?)

PSU CS 311 - Algorithms Analysis and Design 8 Adjacency-matrix representation II n An adjacency matrix can equally well be used for digraphs (directed graphs) n A 2-D array has a mark at [i][j] if there is an edge from node i to node j n Again, this is only suitable for small graphs! A B G E F D C ABCDEFGABCDEFG A B C D E F G

PSU CS 311 - Algorithms Analysis and Design 9 Edge-set representation I n An edge-set representation uses a set of nodes and a set of edges l The sets might be represented by, say, linked lists l The set links are stored in the nodes and edges themselves n The only other information in a node is its element (that is, its value)—it does not hold information about its edges n The only other information in an edge is its source and destination (and attribute, if any) l If the graph is undirected, we keep links to both nodes, but don’t distinguish between source and destination n This representation makes it easy to find nodes from an edge, but you must search to find an edge from a node n This is seldom a good representation

PSU CS 311 - Algorithms Analysis and Design 10 Edge-set representation II n Here we have a set of nodes, and each node contains only its element (not shown) A B G E F D C p q r s t u v w n Each edge contains references to its source and its destination (and its attribute, if any) edgeSet = { p: (A, E), q: (B, C), r: (E, B), s: (D, D), t: (D, A), u: (E, G), v: (F, E), w: (G, D) } nodeSet = {A, B, C, D, E, F, G}

PSU CS 311 - Algorithms Analysis and Design 11 Adjacency-set representation I n An adjacency-set representation uses a set of nodes l Each node contains a reference to the set of its edges l For a directed graph, a node might only know about (have references to) its out-edges n Thus, there is not one single edge set, but rather a separate edge set for each node l Each edge would contain its attribute (if any) and its destination (and possibly its source)

PSU CS 311 - Algorithms Analysis and Design 12 Adjacency-set representation II * n Here we have a set of nodes, and each node refers to a set of edges A B G E F D C p q r s t u v w A { p } B { q } C { } D { s, t } E { r, u } F { v } G { w } n Each edge contains references to its source and its destination (and its attribute, if any) p: (A, E) q: (B, C) r: (E, B) s: (D, D) t: (D, A) u: (E, G) v: (F, E) w: (G, D)

PSU CS 311 - Algorithms Analysis and Design 13 Adjacency-set representation III n If the edges have no associated attribute, there is no need for a separate Edge class l Instead, each node can refer to a set of its neighbors l In this representation, the edges would be implicit in the connections between nodes, not a separate data structure n For an undirected graph, the node would have references to all the nodes adjacent to it n For a directed graph, the node might have: l references to all the nodes adjacent to it, or l references to only those adjacent nodes connected by an out-edge from this node

PSU CS 311 - Algorithms Analysis and Design 14 Adjacency-set representation IV n Here we have a set of nodes, and each node refers to a set of other (pointed to) nodes n The edges are implicit A B G E F D C A { E } B { C } C { } D { D, A } E { B, G } F { E } G { D }

PSU CS 311 - Algorithms Analysis and Design n A graph is a pair G = (V, E), where l V is a set E is a set of pairs (v, w) with v, w ∈ V n We call the elements of V the vertices of G; n the elements of E are the edges of G. Graphs Vertices a and c are adjacent; vertices f and g are not. Vertex b and edge (b, g) are incident; vertex e and edge (d, g) are not. e a f d b c g Example: V = {a, b, c, d, e, f, g} E = {(a, c), (b, d), (b, e), (b, g), (d, e), (d, g), (e, f)} The neighbourhood of vertex b is the set N(b) = {d, e, g}. The degree deg(b) of vertex b is 3.

PSU CS 311 - Algorithms Analysis and Design Undirected vs. Directed Graphs e a f d b c g In an undirected graphs, edges are unordered pairs (v, w). That is, edges (v, w) and (w, v) are the same edge; there is no sense of direction. e a f d b c g In a directed graph, edges are ordered pairs (v, w). That is, edges (v, w) and (w, v) are two different edges. Edge (v, w) is directed from v to w. Vertex v is the source of edge (v, w); vertex w is its target.

PSU CS 311 - Algorithms Analysis and Design Paths and Cycles A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1,v i ) ∈ E. Path P is simple if no vertex appears more than once in P. g f i d e b a h j c

PSU CS 311 - Algorithms Analysis and Design Paths and Cycles A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1,v i ) ∈ E. Path P is simple if no vertex appears more than once in P. P 1 = (g, d, e, b, d, a, h) is not simple. g f i d e b a h j c P 2 = (f, i, c, j) is simple.

PSU CS 311 - Algorithms Analysis and Design Paths and Cycles A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1,v i ) ∈ E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v 0, v 1, …, v k – 1 ) such that, for 0 ≤ i < k, edge (v i, v (i + 1) mod k ) in E. Cycle C is simple if path (v 0, v 1, …, v k – 1 ) is simple. C 1 = (a, h, j, c, i, f, g, d) is simple. g f i d e b a h j c g f i d e b a h j c

PSU CS 311 - Algorithms Analysis and Design Paths and Cycles A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1,v i ) ∈ E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v 0, v 1, …, v k – 1,, v 0 ) such that, for 0 ≤ i < k, edge (v i, v (i + 1) mod k ) ∈ E. Cycle C is simple if path (v 0, v 1, …, v k – 1 ) is simple. g f i d e b a h j c C 2 = (g, d, b, h, j, c, i, e, b) is not simple. g f i d e b a h j c C 1 = (a, h, j, c, i, f, g, d) is simple.

PSU CS 311 - Algorithms Analysis and Design A graph H = (W, F) is a subgraph of a graph G = (V, E) if W in V and F in E. Subgraphs

PSU CS 311 - Algorithms Analysis and Design A graph H = (W, F) is a subgraph of a graph G = (V, E) if W ⊆ V and F ⊆ E. Subgraphs

PSU CS 311 - Algorithms Analysis and Design Spanning Graphs A spanning graph of G is a subgraph of G that contains all vertices of G.

PSU CS 311 - Algorithms Analysis and Design Connectivity The connected components of a graph are its maximal connected subgraphs. A graph G is connected if there is a path between any two vertices in G.

PSU CS 311 - Algorithms Analysis and Design Trees and Forests A tree is a graph that is connected and contains no cycles. A forest is a graph that contains no cycles. The connected components of a forest are trees.

PSU CS 311 - Algorithms Analysis and Design Spanning Trees and Spanning Forests A spanning tree of a graph is a spanning graph that is a tree. A spanning forest of a graph is a spanning graph that is a forest.

PSU CS 311 - Algorithms Analysis and Design Graph Representations incidency list representation: u y w v x z c b e d a b c d ea u vwxyz

PSU CS 311 - Algorithms Analysis and Design 28 Searching a graph n With certain modifications, any tree search technique can be applied to a graph l This includes depth-first, breadth-first, depth-first iterative deepening, and other types of searches n The difference is that a graph may have cycles l We don’t want to search around and around in a cycle n To avoid getting caught in a cycle, we must keep track of which nodes we have already explored n There are two basic techniques for this: l Keep a set of already explored nodes, or l Mark the node itself as having been explored

PSU CS 311 - Algorithms Analysis and Design 29 Example: Depth-first search n Here is how to do DFS on a tree: Put the root node on a stack; while (stack is not empty) { remove a node from the stack; if (node is a goal node) return success; put all children of the node onto the stack; } return failure; n Here is how to do DFS on a graph: Put the starting node on a stack; while (stack is not empty) { remove a node from the stack; if (node has already been visited) continue; if (node is a goal node) return success; put all adjacent nodes of the node onto the stack; } return failure;

PSU CS 311 - Algorithms Analysis and Design 30 Finding connected components n A depth-first search can be used to find connected components of a graph l A connected component is a set of nodes; therefore, l A set of connected components is a set of sets of nodes n To find the connected components of a graph: while there is a node not assigned to a component { put that node in a new component do a DFS from the node, and put every node reached into the same component }

PSU CS 311 - Algorithms Analysis and Design 31 Graph applications n Graphs can be used for: l Finding a route to drive from one city to another l Finding connecting flights from one city to another l Determining least-cost highway connections l Designing optimal connections on a computer chip l Implementing automata l Implementing compilers l Doing garbage collection l Representing family histories l Pert charts l Playing games

PSU CS 311 - Algorithms Analysis and Design 32 Shortest-path n Suppose we want to find the shortest path from node X to node Y n It turns out that, in order to do this, we need to find the shortest path from X to all other nodes l Why? l If we don’t know the shortest path from X to Z, we might overlook a shorter path from X to Y that contains Z n Dijkstra’s Algorithm finds the shortest path from a given node to all other reachable nodes

PSU CS 311 - Algorithms Analysis and Design 33 Dijkstra’s algorithm I n Dijkstra’s algorithm builds up a tree: there is a path from each node back to the starting node n For example, in the following graph, we want to find shortest paths from node B n Edge values in the graph are weights n Node values in the tree are total weights n The arrows point in the right direction for what we need A B EF DC 3 5 1 5 2 45 1 A B EF DC 3 4 6 89 0

PSU CS 311 - Algorithms Analysis and Design 34 Dijkstra’s algorithm II n For each vertex v, Dijkstra’s algorithm keeps track of three pieces of information: l A boolean telling whether we know the shortest path to that node (initially true only for the starting node) l The length of the shortest path to that node known so far (0 for the starting node) l The predecessor of that node along the shortest known path (unknown for all nodes) n Dijkstra’s algorithm proceeds in phases—at each step: l From the vertices for which we don’t know the shortest path, pick a vertex v with the smallest distance known so far l Set v ’s “known” field to true l For each vertex w adjacent to v, test whether its distance so far is greater than v ’s distance plus the distance from v to w ; if so, set w ’s distance to the new distance and w ’s predecessor to v

PSU CS 311 - Algorithms Analysis and Design 35 Dijkstra’s algorithm III n Three pieces of information for each node: + if the minimum distance is known for sure l The best distance so far l The node’s predecessor A B EF DC 3 5 1 5 2 45 1 node A B C D E F init’ly inf 0- inf 1 3B +0- 5B inf 2 +3B +0- 4A inf 3 +3B +0- +4A 6C 8C inf 4 +3B +0- +4A +6C 8C 11D 5 +3B +0- +4A +6C +8C 9E 6 +3B +0- +4A +6C +8C +9E A B EF DC 3 4 6 89 0

PSU CS 311 - Algorithms Analysis and Design 36 Summary n A graph may be directed or undirected n The edges (=arcs) may have weights or contain other data, or they may be just connectors n Similarly, the nodes (=vertices) may or may not contain data n There are various ways to represent graphs l The “best” representation depends on the problem to be solved l You need to consider what kind of access needs to be quick or easy n Many tree algorithms can be modified for graphs l Basically, this means some way to recognize cycles

PSU CS 311 - Algorithms Analysis and Design 37 A graph puzzle n Suppose you have a directed graph with the above shape l You don’t know how many nodes are in the leader l You don’t know how many nodes are in the loop l You don’t know how many nodes there are total Devise an O(n) algorithm ( n being the total number of nodes) to decide when you must already be in the loop l This is not asking to find the first node in the loop l You can only use a fixed (constant) amount of extra memory l You cannot mark the graph as you go nodes in leadernodes in loop start here