Definitions Graph: Collection of nodes and edges Adjacent: Two nodes are adjacent if they are connected by a single edge Path: A sequence of adjacent vertices Non-directed: Edge (v,w) implies (w,v) Directed: Edge (v,w) doesn’t imply (w,v) Weighted: Edges have weights associated with them. Connected: There is at least one path from any vertex to any other vertex.
Representing Vertices Adjacency Matrix typedef struct Graph { Vertex *vertexList; EDGE adjMat[V][V]; } Adjacency List typedef struct Graph { VERTEX *vertexList; } // Note: For adjacency lists, each vertex contains a linked list of adjacent edges Sample Nodes (differs depending on the representation) typedef struct VERTEX_LNK { int vertex; int color; int weight; struct *EDGE_LNK edges; } VERTEX typedef struct EDGE_LNK { char *label; int weight; struct Edge_LNK *ptr; } EDGE
Adjacency Matrix Two dimension array of rows and columns Works well for an a graph that is dense For graphs that are sparse, lots of overhead is incurred skipping over null edges ABCD ANull3 null BNull 4 C2 DnullNull57 Dense graph: One in which has more than half of its possible edges declared A B D C
Adjacency Lists One dimension array of linked lists Works well for an a graph that is sparse For graphs that are dense, lots of overhead is incurred because we loose direct access capability For dense graphs, more A B C D A B D C B,3 C,4 A,2 C,5 D,7
Parallel Arrays Parallel arrays has the advantage of making a good use of memory for both sparse and dense graphs, while preserving the direct access of edges Parallel arrays make it easy to send graphs between computers NodesABCD Edge offsets Edge weights Edge nodes BCACD A B D C
Graph Algorithms Traversal –Depth First For all Vertices, clear colors depthFirstSearch(Vertex v) If not null Then Visit() and color the Vertex push all unvisited adjacent vertices onto the stack While stack not empty pop a vertex, newVertex, from the stack depthFirstSearch(newVertex) –Breadth First (Same as depth first except use a queue) –Complexity is O(E+V)
Minimum Spanning Tree Minimum edge set of a graph that visits all edges |T|=|V|-1 Undirected – DFS or BFS; record vertices and unvisited neighbors Directed – Algorithm 1 (Kruskal’s Algorithm) Sort the edges (O(E lg E) For each of the sorted edges {(O(~E) with Union Find Algorithm} If a cycle is not created, add the edge to the spanning tree Directed – Algorithm 2 (Prim’s Algorithm) Create a priority queue (StartV Key=0, OtherVertices Key = infinity) While priority queue is not empty min = RemoveMin() For each vertex adj adjacent to min If weight(min,adj)< key(adj) Then adjustKey(adj) and set Predecessor[adj] = min Complexities O(ElgE) Kruskal and O(lgV*(V+E))Prim
Topological Sort Critical Path Analysis Application, sequence of courses where prerequisites are met Limitations: Graph cannot have cycles (v1,v2, …,vk,v1) Algorithm Let G=[V,E] be a clone of the original graph While V > 0 vertex = FindVertexWithNoSuccessors() If found Then Call delete(vertex) sortedArray[--V] = vertex Else Print error message; graph has cycles
Shortest Path Requirements: Directed Graph with positive weights Step 1: Create a shortest path array, short, with each entry having an infinite weight Except the original vertex that has a weight of zero. Step 2: Create a path array, path, that will indicate the shortest path from one vertex to another Step 3: The Algorithm While more vertices are left Find the remaining vertex, light, with the lightest weight For each edge, (light,w), of light If edge weight w(light,w)+short(light) < short(light) Then short[light] = w(light,w) + short(light) path[light] = w Complexity: O(V+lg V*V+lgV*E) = O(lgV*(V+E)) if a heap is used to order, remove-min, and relax weights.