SymbolGraph.cs
// switched to C# Dictionary in spite of SequentialSymbolTable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SymbolGraphExample { public class SymbolGraph { // private SequentialSearchST<String, int> st; // string -> index private Dictionary<string, int> d; private string[] keys; // index -> string private Graph graph; // the underlying graph public SymbolGraph(string filepath, char delimiter) { //st = new SequentialSearchST<String, int>(); d = new Dictionary<string, int>(); try { using (StreamReader sr = new StreamReader(filepath)) { while (sr.Peek() >= 0) { string s = sr.ReadLine(); string[] substrings = s.Split(delimiter); foreach (var substring in substrings) { //if (!st.contains(substring)) if (!d.ContainsKey(substring)) { //st.put(substring, st.size()+1); int c = d.Count; d.Add(substring, c); //Console.WriteLine(substring+" "+st.size()); Console.WriteLine(substring + " " + c); //Console.WriteLine(); //st.ConsoleDisplay(); } } } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); }; // inverted index to get string keys in an aray //keys = new string[st.size()]; keys = new string[d.Count]; foreach (KeyValuePair<string, int> entry in d) { // do something with entry.Value or entry.Key keys[entry.Value] = entry.Key; } //test //foreach (string k in keys) Console.WriteLine(k); /* deprecated foreach (string name in st.keys()) { keys[st.get(name)] = name; } */ // building graph in second reading file // graph = new Graph(st.size()); graph = new Graph(d.Count); try { using (StreamReader sr = new StreamReader(filepath)) { while (sr.Peek() >= 0) { string s = sr.ReadLine(); string[] substrings = s.Split(delimiter); //int v = st.get(substrings[0]); //int v = d.FirstOrDefault(x=>x.Key== substrings[0]).Value; //st.get(substrings[0]); int v = d[substrings[0]]; for (var i = 1; i < substrings.Length; i++) { //int w -1; //int w = d.First(x => x.Key == substrings[i]).Value; //st.get(substrings[i]); int w = d[substrings[i]]; //if (w!=v) graph.addEdge(v,w); } } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); }; } /** * Does the graph contain the vertex named {@code s}? * @param s the name of a vertex * @return {@code true} if {@code s} is the name of a vertex, and {@code false} otherwise */ public bool contains(String s) { // return d.FirstOrDefault(x => x.Key == s).Key!=null; //st.contains(s); return d.ContainsKey(s); //st.contains(s); } /** * Returns the integer associated with the vertex named {@code s}. * @param s the name of a vertex * @return the integer (between 0 and <em>V</em> - 1) associated with the vertex named {@code s} */ public int indexOf(String s) { return d.FirstOrDefault(x => x.Key == s).Value;//st.get(s); } /** * Returns the name of the vertex associated with the integer {@code v}. * @param v the integer corresponding to a vertex (between 0 and <em>V</em> - 1) * @throws IllegalArgumentException unless {@code 0 <= v < V} * @return the name of the vertex associated with the integer {@code v} */ public String nameOf(int v) { validateVertex(v); return keys[v]; } /** * Returns the graph assoicated with the symbol graph. It is the client's responsibility * not to mutate the graph. * @return the graph associated with the symbol graph */ public Graph getGraph() { return graph; } // throw an IllegalArgumentException unless {@code 0 <= v < V} private void validateVertex(int v) { int V = graph.V; if (v < 0 || v >= V) throw new ArgumentException("vertex " + v + " is not between 0 and " + (V - 1)); } public string toString() { StringBuilder s = new StringBuilder(); s.Append(graph.V + " vertices, " + graph.E + " edges " + "\n"); for (int v = 0; v < graph.V; v++) { s.Append(nameOf(v) + ": "); foreach (int w in graph.adjVerticles(v)) { s.Append(nameOf(w) + " "); } s.Append("\n"); } return s.ToString(); } } } |
SymbolTable – doesn’t work to symbol graph because of contains method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SymbolGraphExample { class SymbolTable { } public class SequentialSearchST<Key, Value> : IEnumerable<Key> where Value:IComparable { private int n; // number of key-value pairs private Node first; // the linked list of key-value pairs // a helper linked list data type private class Node { public Key key { get; set; } public Value val { get; set; } public Node next { get; set; } public Node(Key key, Value val, Node next) { this.key = key; this.val = val; this.next = next; } } /** * Initializes an empty symbol table. */ public SequentialSearchST() { } /** * Returns the number of key-value pairs in this symbol table. * * @return the number of key-value pairs in this symbol table */ public int size() { return n; } /** * Returns true if this symbol table is empty. * * @return {@code true} if this symbol table is empty; * {@code false} otherwise */ public bool isEmpty() { return size() == 0; } /** * Returns true if this symbol table contains the specified key. * * @param key the key * @return {@code true} if this symbol table contains {@code key}; * {@code false} otherwise * @throws IllegalArgumentException if {@code key} is {@code null} */ public bool contains(Key key) { if (key == null) throw new ArgumentException("argument to contains() is null"); Value r = get(key); return (r.CompareTo(default(Value)) != 0); } /** * Returns the value associated with the given key in this symbol table. * * @param key the key * @return the value associated with the given key if the key is in the symbol table * and {@code null} if the key is not in the symbol table * @throws IllegalArgumentException if {@code key} is {@code null} */ public Value get(Key key) { if (key == null) throw new ArgumentException("argument to get() is null"); for (Node x = first; x != null; x = x.next) { if (key.Equals(x.key)) return x.val; } return default(Value); } /** * Inserts the specified key-value pair into the symbol table, overwriting the old * value with the new value if the symbol table already contains the specified key. * Deletes the specified key (and its associated value) from this symbol table * if the specified value is {@code null}. * * @param key the key * @param val the value * @throws IllegalArgumentException if {@code key} is {@code null} */ public void put(Key key, Value val) { if (key == null) throw new ArgumentException("first argument to put() is null"); if (val == null) { delete(key); return; } for (Node x = first; x != null; x = x.next) { if (key.Equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); n++; } /** * Removes the specified key and its associated value from this symbol table * (if the key is in this symbol table). * * @param key the key * @throws IllegalArgumentException if {@code key} is {@code null} */ public void delete(Key key) { if (key == null) throw new ArgumentException("argument to delete() is null"); first = delete(first, key); } // delete key in linked list beginning at Node x // warning: function call stack too large if table is large private Node delete(Node x, Key key) { if (x == null) return null; if (key.Equals(x.key)) { n--; return x.next; } x.next = delete(x.next, key); // recursion return x; } // iteraror System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator<Key> GetEnumerator() { var node = first; while (node != null) { yield return node.key; node = node.next; } } /** * Returns all keys in the symbol table as an {@code Iterable}. * To iterate over all of the keys in the symbol table named {@code st}, * use the foreach notation: {@code for (Key key : st.keys())}. * * @return all keys in the symbol table */ public IEnumerable<Key> keys() { Queue<Key> queue = new Queue<Key>(); for (Node x = first; x != null; x = x.next) queue.Enqueue(x.key); return queue; } // public void ConsoleDisplay() { Console.WriteLine(); Console.WriteLine("key" + " " + "val"); for (Node n = first; n != null; n = n.next) { Console.WriteLine(" " + n.key + " " + n.val); } } } } |
Graph.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SymbolGraphExample { public class Graph { // private static final String NEWLINE = System.getProperty("line.separator"); public int V { get; set; } public int E { get; set; } public Bag<int>[] adj { get; set; } /** * Initializes an empty graph with {@code V} vertices and 0 edges. * param V the number of vertices * * @param V number of vertices * @throws IllegalArgumentException if {@code V < 0} */ public Graph(int V) { if (V < 0) throw new ArgumentException("Number of vertices must be nonnegative"); this.V = V; this.E = 0; adj = new Bag<int>[V]; for (int v = 0; v < V; v++) { adj[v] = new Bag<int>(); } } /** * Initializes a graph from the specified input stream. * The format is the number of vertices <em>V</em>, * followed by the number of edges <em>E</em>, * followed by <em>E</em> pairs of vertices, with each entry separated by whitespace. * * @param in the input stream * @throws IllegalArgumentException if the endpoints of any edge are not in prescribed range * @throws IllegalArgumentException if the number of vertices or edges is negative * @throws IllegalArgumentException if the input stream is in the wrong format */ public Graph(string filepath) { try { using (StreamReader sr = new StreamReader(filepath)) { int i = 0; while (sr.Peek() >= 0) { // reading number of verticles if (i == 0) { V = Convert.ToInt32(sr.ReadLine()); adj = new Bag<int>[V]; for (int v = 0; v < V; v++) { adj[v] = new Bag<int>(); } i++; } // reading number of edges else if (i == 1) { E = Convert.ToInt32(sr.ReadLine()); if (E < 0) throw new ArgumentException("number of edges in a Graph must be nonnegative"); i++; } else { // for (int j = 0; j < E; j++) // { string s = sr.ReadLine(); Char delimiter = ' '; String[] substrings = s.Split(delimiter); int v = Convert.ToInt32(substrings[0]); int w = Convert.ToInt32(substrings[1]); validateVertex(v); validateVertex(w); addEdge(v, w); } } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); }; } // throw an IllegalArgumentException unless {@code 0 <= v < V} public void validateVertex(int v) { if (v < 0 || v >= V) throw new ArgumentException("vertex " + v + " is not between 0 and " + (V - 1)); } /** * Adds the undirected edge v-w to this graph. * * @param v one vertex in the edge * @param w the other vertex in the edge * @throws IllegalArgumentException unless both {@code 0 <= v < V} and {@code 0 <= w < V} */ public void addEdge(int v, int w) { validateVertex(v); validateVertex(w); E++; adj[v].push(w); adj[w].push(v); } /** * Returns the vertices adjacent to vertex {@code v}. * * @param v the vertex * @return the vertices adjacent to vertex {@code v}, as an iterable * @throws IllegalArgumentException unless {@code 0 <= v < V} */ public IEnumerable<int> adjVerticles(int v) { validateVertex(v); return adj[v]; } /** * Returns the degree of vertex {@code v}. * * @param v the vertex * @return the degree of vertex {@code v} * @throws IllegalArgumentException unless {@code 0 <= v < V} */ public int degree(int v) { validateVertex(v); return adj[v].size(); } /** * Returns a string representation of this graph. * * @return the number of vertices <em>V</em>, followed by the number of edges <em>E</em>, * followed by the <em>V</em> adjacency lists */ public String toString() { StringBuilder s = new StringBuilder(); s.Append(V + " vertices, " + E + " edges " + "\n"); for (int v = 0; v < V; v++) { s.Append(v + ": "); foreach (int w in adj[v]) { s.Append(w + " "); } s.Append("\n"); } return s.ToString(); } } } |
Bag.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SymbolGraphExample { public class Bag<T> : IEnumerable<T> { Node<T> first; private int N; class Node<T> { public T item; public Node<T> next; } public bool isEmpty() { return (first == null) || (N == 0); } public int size() { return N; } public void push(T item) { // adding to the begining Node<T> oldfirst = first; first = new Node<T>(); first.item = item; first.next = oldfirst; N++; } /* // no pop in stack method public T pop() { T item = first.item; first = first.next; N--; return item; } */ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator<T> GetEnumerator() { var node = first; while (node != null) { yield return node.item; node = node.next; } } } } |
Test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SymbolGraphExample { class Program { static void Main(string[] args) { SymbolGraph sg = new SymbolGraph("Airports.txt", ' '); Console.WriteLine(sg.toString()); Console.ReadLine(); } } } |