Graphs

1 / 64

# Graphs - PowerPoint PPT Presentation

##### Graphs

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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Graphs

2. Chapter 12 • Graphs • Overview • Graphs provide a rich and powerful way to model relations.

3. Chapter Objectives • 1. To review basic graph vocabulary and concepts. • 2. To develop classes that model various kinds of graphs. • 3. To learn some useful algorithms for manipulating graphs.

4. Graphs • Graphs represent many real-world problems • They do so by showing complex relationships between objects

5. Course prerequisites

6. Prerequisites in Graphical Form

7. Tables and graphs • One is commonly used to represent the other. • Both have a two-dimensional quality • Both can be used to represent relationships between objects

8. Graph Terminology • Graph - represents relationship among nodes • Vertex - a node in a graph • Edge - link connecting 2 nodes (relationship) • Adjacent - 2 nodes connected by an edge • Undirected Graph - a graph in which the edges have no directional component

9. Terminology (con’t) • Network - a graph in which the edges are weighted • Directed graph (digraph) - a graph in which the edges have directional components (like the course prerequisites example)

10. Terminology (con’t) • Path - a sequence of distinct vertices, each adjacent to the next • Cycle - an undirected path containing at least three vertices such that the last vertex is adjacent to the first • Connected graph - an undirected graph in which there is a path from any vertex to any other vertex

11. Types of graphs

12. Terminology (con’t) • Disconnected Graph - an undirected graph in which it is impossible to get to at least one node from any other vertex • Free tree - a connected, undirected graph with no cycles

13. examples

14. Directed cycle - a directed path which is cyclical

15. Strongly connected - a digraph in which there is a directed path from any vertex to any other vertex

16. Weakly connected - a digraph in which it is not possible to go from any vertex to any other

17. Graph theory • Graphs may be most conveniently defined in terms of sets. • First, we have a set of vertices (V) • Second, we have a set of edges (E) • A graph is a set of V and E

18. Adjacency • If there is an edge connection two nodes then they are adjacent. • Note: if the edge is directed, then there is only adjacency one way. • Let Av be the set of all vertices adjacent to a given vertex v

19. Adjacency • For the subsets Av, we can construct the edges as ordered pairs by the rule: • The pair (v,w) is an edge if and only if w is a member of Av.

20. An Example Digraph

21. Vertices Each vertex is part of the set V of vertices in the graph V = {0,1,2,3,4}

22. Edges Each edge can be represented as a pair of vertices E = {{0,3}, {1,0}, {1,2}, {1,4}, {2,1}, {4,0},{4,2}}

23. Graphs and set theory • We can keep track of all the edges in a graph by keeping track, for all vertices v, of the set of edges containing v (E). • Similarly, we could keep track of the set A of all vertices adjacent to v.

24. Pairs adjacent to vertex 0 (A0) The vertices adjacent to vertex 0 are the set A0 = {3}

25. New graph definition • This leads us to a new definition of a graph: • A graph G consists of a set V, called the vertices of G, and, for all v in V, a subset A of V, consisting of the set of vertices adjacent to v.

26. Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {3} A1 = {0,2,4} A2 = {1} A3 = {} A4 = {0,2}

27. Undirected graphs • This definition works with undirected graphs as well: • An undirected graph is one which satisfies the following symmetry property: • if w is a member of Av, this implies that v is a member of Aw, for all v and w such that v,w are members of V

28. Undirected Graph

29. Vertices Each vertex is part of the set V of vertices in the graph V = {0,1,2,3,4}

30. Edges Each edge can be represented as a pair of vertices E = {{0,1}, {0,3}, {0,4}, {1,2}, {1,4}, {2,4}}

31. Pairs adjacent to 0 (A0) The vertices adjacent to vertex 0 are the set A0 = {1,3,4}

32. Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {1,3,4} A1 = {0,2,4} A2 = {1} A3 = {0} A4 = {0,1}

33. How to represent graphs • Since graphs are sets and subsets, they need a two dimensional structure • One dimension is the vertices • The other dimension is the edges (or the vertices adjacent to the one we are on)

34. What might do with graphs? • Neighborhood queries • For a vertex, find all edges connected to other vertices • Edge member queries • Determine whether an edge is in the graph

35. Analysis • If there are n vertices • and m edges • it may take as many as n*m operations to traverse every relationship represented in a graph • Depending on our representation of the graph we may be able to streamline the searching process

36. List representation • One method of implementing a graph is to employ a List of vertices, such that from each vertex we can have access to those which are adjacent. • A list of linked lists is one method of doing this.

38. Adjacency Lists • Linked implemetation • Advantages • No wasted memory space • Disadvantages • More complicated. • Sequential access only. • Search would be O(n) where n is the number of possible vertices adjacent to any given vertex.

39. Contiguous implementation • Advantages • Less awkward than linked • Allows for random access

41. Adjacency Tables • Advantages • Easy to interpret. • A[v,w] is true only if vertex v is adjacent to vertex w. • If the graph is directed then this indicates an edge from v to w. • If the graph is undirected then the adjacency table is symmetric and A[w,v] will also be true.

42. Disadvantages • Mirrored across the diagonal for undirected graphs • Not sparse.

43. UAL GraphADT • Characteristics • An Undirected Adjacency List Graph G = (V,E) stores an undirected graph so that vertex neighbors can be found efficiently. • The number of vertices in the graph, n = |V|, is fixed when the graph is created. • The vertices are labeled 0…n-1.

44. Operations:

45. vertexSize() • int vertexSize() • Precondition: None. • Postcondtion: None. • Returns: The number of vertices in the graph, |V|.

46. edgeSize() • int edgeSize() • Precondition: None. • Postcondtion: None. • Returns: The number of edges in the graph, |E|.