CS201 - Spring 2014 - Class 32

  • graphs
       - A graph is a set of vertices (or nodes) V and a set of edges (u,v) in E where u, v are in V
       - there are different types of graphs (show some examples)
          - undirected
             - if an edge (u, v) exists (that is u is connected to v) then (v, u) also exits, that is, v is connected to u
             - when drawn, we'd draw the edge between u and v as a line
          - directed
             - edges have a direction, that is, u may be connected to v, but not necessarily vice versa
             - when drawn, we'd draw the edge from u to v as an "arrow"
          - weighted
             - each graph edge has an associated weight
             - can have either weighted, undirected graphs or weighted, directed graphs

       - graph terminology
          - path
             - a path is a list of vertices p_1, p_2, ..., p_k where there exists an edge (p_i, p_{i+1}) in E
             - intuitively, it's a connection of edges
             - a "simple" path is a path where all vertices are unique, except possibly the start and end
          - cycle
             - a simple path with p_1 = p_k AND where the edges are unique (this avoids the problem of calling A, B, A a cycle in an undirected graph)
             - look at cycles in directed and undirected graphs
             - a graph is called "acyclic" if it doesn't have any cycles in it
          - connected
             - a graph is connected if every pair of vertices is connected by a path
             - for directed graphs, this is called "strongly connected"
             - "weakly connected"?
                - if replacing all of the directed edges with undirected edges leaves a connected graph

       - where have we seen graphs in this class already?
          - tree
             - undirected
             - acyclic
             - note that we have to specify a root

       - some other special cases
          - twig
          - ring
          - complete graph
             - an edge from every node to every other node
          - bipartite graph
             - a graph where every vertex can be partitioned into two disjoint sets X and Y, such that all edges connect a vertex u in X to a vertex v in Y   

       - what are some questions we might want to ask about graphs?
          - does it have a cycle?
          - is it connected? strongly connected? weakly connected?
          - is there a path from a to b?
          - what is the shortest path from a to b?
          - do graphs have the same structure (these are called isomorphic graphs)?

       - where do graphs come up in real life?
          - transportation networks (flights, roads, etc.)
             - flights
                - directed or undirected?
                - weighted?
                - what are the weights?
             - google maps
                - directed or undirected
                - weighted?
                - what are the weights?
          - communication networks
             - computer networks
             - phone networks
          - web
             - what are the vertices and edges?
                - vertices are web pages
                - edges are links
          - social networks
             - what are the vertices and edges?
                - vertices are people
                - edges are relationships (e.g. friends on facebook)
          - circuit design
          - bayesian networks

  • representing graphs
       - so far, we've drawn them on the board fine, but how are we going to store them for processing?
       - adjacency list
          - each vertex u in V contains a linked list of all the vertices v such that there exists an edge (u, v) in E, that is that there is an edge from u to v
          A: B->D
          B: A->D
          C: D
          D: A->B->C->E
          E: D

       - adjacency matrix
          - a |V| by |V| matrix A, such that A_ij is 1 if edge (i, j) is in E, 0 otherwise

           A B C D E
          A 0 1 0 1 0
          B 1 0 0 1 0
          C 0 0 0 1 0
          D 1 1 1 0 1
          E 0 0 0 1 0

          - what will this matrix look like if the graph is undirected?
             - it will be symmetric
       - examples:
          - draw the following graphs
             -- (undirected)
             A: B C
             B: A C
             C: A B
             -- (directed)
             A: D
             B: D E
             C: D B
             D: A B C D E
             E: B C
             -- (directed)
              A B C
             A 1 0 0
             B 0 0 1
             C 0 1 0
       - how would we incorporate weights into both of these approaches?
          - adjacency list: just keep that additional piece of information in the linked list
          - adjacency matrix: store that value in the matrix (instead of just a 0 or a 1)
       - What are the benefits/drawbacks of each approach and when might each be useful?
          - adjacency list
             - good for sparse graphs
             - more space efficient (for sparse graphs)
             - must traverse the adjacency list to discover if an edge exists
          - adjacency matrix
             - good for dense graphs
             - constant time lookup to discover if an edge exists
             - for non-weighted graphs, only requires a boolean matrix
       - Can we get the best of both worlds (constant lookup, good sparse representation)?
       - sparse adjacency matrix
          - rather than storing adjacent vertices as a linked list, store as a hashtable
          - benefits/drawbacks?
             - constant time lookup
             - fairly space efficient (though some overhead with keeping the table)
             - not good for dense graphs

  • finding cycles
       - given a connected, undirected graph, how can we determine if it has a cycle in it?
          - or, given a connected graph, determine that it is not a tree
       - what is the definition of a cycle?
          - a path, where the endpoints are the same
       - idea:
          - start at a node, go down a path
             - stop when either we find a vertex on the path that we've already seen
             - or when we hit a dead-end
          - if we hit a dead-end, backtrack and find another path
          - if we visit all of the nodes, without finding a repeat vertex, it's acyclic
       - does this sound like anything we've seen before?
          - depth first search!

       - what modifications need to be made?
          - if we visit a node that we've already visited, then we've found a cycle
          - what about where we just came from?
             - need to know where we came from so we can avoid calling that a cycle
          - want to return true if we find a cycle, false otherwise