1 / 17

Graphs

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.

ashton
Télécharger la présentation

Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs CIS 237 – Data Structures

  2. 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}

  3. City Hall Library Court House Opera Human Services Sample – Government Buildings • subgraph • adjacent vertices (neighbors) • path/length of the path

  4. Categories of Graphs • Graph • connected • disconnected • complete • Directed Graph (diagraph) • in-degree • out-degree • directed path • weakly or strongly connected • weighted

  5. 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 }

  6. 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

  7. Users of dfsVisit • Users • depth first visit • topological sort • cycle check • Use • send a list • traverse the list on return or check return type

  8. 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

  9. 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

  10. 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++

  11. 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

  12. 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

  13. 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);

  14. 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

  15. 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

  16. 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)

  17. 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

More Related