170 likes | 313 Views
Graphs. CIS 237 – Data Structures. Graphs. a set of vertices V a set of edges E an edge (v i , v j ) connects v i and v j a self-loop Vertices = {v 1 , v 2 , v 3 , … , v m } Edges = {e 1 , e 2 , e 3 , … , e n }. City Hall. Library. Court House. Opera. Human Services.
E N D
Graphs CIS 237 – Data Structures
Graphs • a set of vertices V • a set of edges E • an edge (vi, vj) connects vi and vj • a self-loop • Vertices = {v1, v2, v3, …, vm} • Edges = {e1, e2, e3, …, en}
City Hall Library Court House Opera Human Services Sample – Government Buildings • subgraph • adjacent vertices (neighbors) • path/length of the path
Categories of Graphs • Graph • connected • disconnected • complete • Directed Graph (diagraph) • in-degree • out-degree • directed path • weakly or strongly connected • weighted
Breadth First Visit for all vertices,v, in G { if v’s color is WHITE set v’s color to GRAY push v on Q while Q not empty { get u from front and pop Q for all vertices,w, adjacent to u (neighbors) { if w’s color is WHITE set w’s color to GRAY push u on Q } set u’s color to BLACK process u }
dfv – depth first visit Void dfv(T v, list<T> &dfsList) { for all vertices, u, adjacent to v { if u’s color is WHITE { set u’s color to GRAY dfv(u, dfsList) } } set v’s color to BLACK push_front v on list
Users of dfsVisit • Users • depth first visit • topological sort • cycle check • Use • send a list • traverse the list on return or check return type
0 A 0 0 0 0 1 0 0 0 0 1 B 1 A D C B F E 0 0 0 1 C 1 0 0 0 0 0 0 0 D 1 E 0 1 0 0 0 0 0 F 0 0 0 0 Representation- Adjacency Matrix A B C D F E
B 7 A 7 3 1 B C 4 E 3 4 2 6 C 7 E 2 D 8 8 D E B F A C D E F 1 D 6 F D 7 Representation – Adjacency Set
Creating the Graph • Insert Vertex • vertices[v] = info • numVertices++ • Insert Edge • make sure the to and from vertex are there • create a neighbor with the to edge and weight • add neighbor to from vertex’s edge set • add to the to vertex’s inDegree • numEdges++
For all vertices in the graph…. vertexMap::iterator itr for (itr = vertices.begin(); itr != vertices.end(); itr++) process *itr Recall itr isiterator in a map itr->first ------ data itr->second ------ vertexInfo
For all vertices adjacent to v... set<neighbor<T> > friends = vertices[v].edges set<neighbor<T> >::iterator nitr; for (nitr =friends.begin(); nitr!=friends.end(); nitr++) process *nitr Recall nitr is an iterator in a set of neighbors nitr->data nitr->weight
With dfv void dfv(T v, list<T> &dfsList) { for (nitr=friends.begin(); nitr!=friends.end(); nitr++) { if (vertices[nitr->data].color == vertexInfo<T>::WHITE) { vertices[nitr->data].color = vertexInfo<T>::GRAY dfv(nitr->data, dfsList) } } vertices[v].color = vertexInfo<T>::BLACK dfsList.push_front(v);
Shortest Path shortestPath(from, to, &path) set the parent of from to from set the sumLength of from to 0 while (findMinPathLength(data)) set data’s color to BLACK for all vertices v adjacent to data if (v’s color is WHITE) if (data’s sumLength+ weight of edge(data, v) < v’s totalLength) v’s sumLength= data’s sumLength+ weight of edge(data, v) v’s parent = data end while //place in path list place the values in the path list in reverse order (push front from to to from Return to’s sumLength
Minimal Spanning Tree copy the vertices into the spanning tree find the first vertex, w, in the vertices on a smallest edge set the parent of w to w set the sumLength of w to 0 while (findMinPathLength(data)) set data’s color to BLACK for all vertices v adjacent to data if (v’s color is WHITE) if ( weight of edge(data, v) < v’s sumLength ) v’s sumLength = weight of edge(data, v) v’s parent = data end while
Minimal Spanning Tree (con’t) For all vertices v in G if parent != data insert an edge (parent, data, data’s sumLength) insert an edge (data, parent, data’s sumLength)
Strong Components • a maximal set of vertices that are mutually accessible • Algorithm • execute dfs for graph G producing aList • generate the transpose for G, GT • for each value in aList • mark it GRAY • call dfs on the transpose to get anotherList • insert anotherList into the component set